ubuntu 定时脚本折腾札记



下面是要定期执行的脚本(/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 user command
  9. 17 * * * * root    cd / && run-parts --report /etc/cron.hourly
  10. 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  11. 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  12. 52 6 1 * * root test -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 user command
  9. 17 * * * * root    cd / && run-parts --report /etc/cron.hourly
  10. 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  11. 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  12. 52 6 1 * * root test -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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值