ubuntu 定时脚本折腾札记

来源:互联网 发布:四级网络课程哪个好 编辑:程序博客网 时间:2024/06/05 19:52


下面是要定期执行的脚本(/home/linxd/Desktop/test.sh

  1. #!/bin/bash
  2. # 不知道要不要加环境变量, 鸟哥上说要.
  3. #PATH=/home/linxd/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
  4. #export PATH
  5. date >>  "/home/linxd/Desktop/test.txt"


Linux设置定期执行脚本crontab用法 : 千万千万要记住, 不只是/etc/crontab中要用绝对路径, 连脚本也要用绝对路径

  1. # /etc/crontab: system-wide crontab
  2. # Unlike any other crontab you don't have to run the `crontab'
  3. # command to install the new version when you edit this file
  4. # and files in /etc/cron.d. These files also have username fields,
  5. # that none of the other crontabs do.
  6. SHELL=/bin/sh
  7. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  8. # m h dom mon dow usercommand
  9. 17 ** * *root    cd / && run-parts --report /etc/cron.hourly
  10. 25 6* * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  11. 47 6* * 7roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  12. 52 61 * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
  13. */1 *  * * *    linxd   /home/linxd/Desktop/test.sh

定时脚本没能正确执行:

  • 默认情况下 cron jobs 的 log 在 /var/log/syslog,可以通过 grep CRON /var/log/syslog 来查看【1】。
  • 对于具体执行是否正确以及脚本执行过程中的一些信息,Linux会通过邮件形式发送到该用户【2】,邮件信息信息在 /var/mail/panjinbo
定时脚本调用GUI:

因为这个脚本需要调用Haroopad(GUI)来进行MD文本的编辑,但是Xhost默认不允许别的图形程序显示在当前屏幕上。出现的错误是这样子的:


`[14720:1104/195401:ERROR:browser_main_loop.cc(162)] Running without the SUID sandbox! `

`[14720:1104/195401:ERROR:browser_main_loop.cc(208)] Gtk: cannot open display: `


解决方案:ubuntu 下使用crontab定时执行java程序

在shell脚本程序GUI命令行之前,加入如下代码:export DISPLAY=:0.0 #启动GUI显示


下面是要定期执行的脚本(/home/linxd/Desktop/test.sh

  1. #!/bin/bash
  2. # 不知道要不要加环境变量, 鸟哥上说要.
  3. #PATH=/home/linxd/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
  4. #export PATH
  5. date >>  "/home/linxd/Desktop/test.txt"


Linux设置定期执行脚本crontab用法 : 千万千万要记住, 不只是/etc/crontab中要用绝对路径, 连脚本也要用绝对路径

  1. # /etc/crontab: system-wide crontab
  2. # Unlike any other crontab you don't have to run the `crontab'
  3. # command to install the new version when you edit this file
  4. # and files in /etc/cron.d. These files also have username fields,
  5. # that none of the other crontabs do.
  6. SHELL=/bin/sh
  7. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  8. # m h dom mon dow usercommand
  9. 17 ** * *root    cd / && run-parts --report /etc/cron.hourly
  10. 25 6* * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  11. 47 6* * 7roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  12. 52 61 * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
  13. */1 *  * * *    linxd   /home/linxd/Desktop/test.sh

定时脚本没能正确执行:

  • 默认情况下 cron jobs 的 log 在 /var/log/syslog,可以通过 grep CRON /var/log/syslog 来查看【1】。
  • 对于具体执行是否正确以及脚本执行过程中的一些信息,Linux会通过邮件形式发送到该用户【2】,邮件信息信息在 /var/mail/panjinbo
定时脚本调用GUI:

因为这个脚本需要调用Haroopad(GUI)来进行MD文本的编辑,但是Xhost默认不允许别的图形程序显示在当前屏幕上。出现的错误是这样子的:


`[14720:1104/195401:ERROR:browser_main_loop.cc(162)] Running without the SUID sandbox! `

`[14720:1104/195401:ERROR:browser_main_loop.cc(208)] Gtk: cannot open display: `


解决方案:ubuntu 下使用crontab定时执行java程序

在shell脚本程序GUI命令行之前,加入如下代码:export DISPLAY=:0.0 #启动GUI显示


0 0
原创粉丝点击