以下是我的 job.sh 文件
echo 'Job Started Running'
echo 'password' | sudo ./logstash -f /usr/share/logstash/bin/contact-index-logstash.conf
echo 'Job Ended'
以下是我的sudo crontab -e
* * * * * bash ~/job.sh > ~/log.log 2>&1
以下是我的 cron 状态
> service cron status ● cron.service - Regular background program
> processing daemon Loaded: loaded (/lib/systemd/system/cron.service;
> enabled; vendor preset: enabled) Active: active (running) since Thu
> 2017-11-09 22:09:03 IST; 1h 40min ago
> Docs: man:cron(8) Main PID: 869 (cron)
> Tasks: 1 Memory: 12.3M
> CPU: 1min 40.378s CGroup: /system.slice/cron.service
> └─869 /usr/sbin/cron -f
>
> Nov 09 23:42:01 akshay CRON[6264]: pam_unix(cron:session): session
>opened for user root by (uid=0) Nov 09 23:43:01 akshay CRON[6270]:
> pam_unix(cron:session): session opened for user root by (uid=0)
>Nov 09 23:43:01 akshay CRON[6271]: (root) CMD (bash ~/job.sh > ~/log.log
> 2>&1)
Nov 09 23:43:01 akshay CRON[6270]: pam_unix(cron:session):
> session closed for user root Nov 09 23:44:01 akshay CRON[6293]:
> pam_unix(cron:session): session opened for user root by (uid=0)
Nov 09
> 23:44:01 akshay CRON[6294]: (root) CMD (bash ~/job.sh > ~/log.log
> 2>&1)
当我在 shell 上直接执行 sh 文件时,它运行没有任何错误,但在 crontab 下它不起作用。
答案1
按照建议@steeldriver 的评论事实应该是这样的。
您的job.sh
文件(假设logstash
位于同一位置!):
echo 'Job Started Running'
./logstash -f /usr/share/logstash/bin/contact-index-logstash.conf
echo 'Job Ended'
你的根crontab
:
* * * * * /home/username/job.sh > /home/username/log.log 2>&1
cron
sh
默认情况下运行你的脚本,所以我删除了不必要的bash
子shell – 如果你仍然需要它,只需再次添加它。