我有一个从该网站获得的脚本,并进行了修改以满足我的需要。原帖:Linux Script to check if process is running & act on the result
从 cron 运行脚本时,它总是创建一个新进程。如果我从 shell 运行它,它会正常运行。谁能帮我调试这个问题吗?
脚本
[root@server2 ~]# cat /root/full-migrate.sh
#!/bin/bash
case "$(pidof perl | wc -w)" in
0) echo "Restarting iu-maildirtoimap: $(date)" >> /var/log/iu-maildirtoimap.txt
/usr/local/bin/iu-maildirtoimap -i currentuser.txt -D imap.gmail.com:993 -d -n7 -I&
;;
1) echo "Everything seems okay: $(date)" >> /var/log/iu-maildirtoimap.txt
;;
*) echo "Removed double iu-maildirtoimap: $(date)" >> /var/log/iu-maildirtoimap.txt
kill -9 $(pidof perl | awk '{print $1}')
;;
esac
crontab 作业
[root@server2 ~]# crontab -l
*/1 * * * * /bin/bash /root/full-migrate.sh
来自日志文件:
Removed double iu-maildirtoimap: Tue Dec 30 02:32:37 GMT 2014
Removed double iu-maildirtoimap: Tue Dec 30 02:32:38 GMT 2014
Removed double iu-maildirtoimap: Tue Dec 30 02:32:39 GMT 2014
Everything seems okay: Tue Dec 30 02:32:39 GMT 2014
Restarting iu-maildirtoimap: Tue Dec 30 02:33:01 GMT 2014
Restarting iu-maildirtoimap: Tue Dec 30 02:34:01 GMT 2014
Restarting iu-maildirtoimap: Tue Dec 30 02:35:01 GMT 2014
前 4 个条目是我手动运行的“/bin/bash/root/full-migrate.sh”
最后 3 个来自 crontab。
关于如何调试此问题有什么建议吗?
在撰写本文时:
[root@server2 ~]# $(pidof perl | wc -w)
bash: 13: command not found
[root@server2 ~]# $(pidof perl | awk '{print $1}')
bash: 26370: command not found
最佳答案
您从命令行进行的测试无效,因为您基本上是在执行进程ID,这会给您一个未找到的命令。
您需要通过命令行进行测试:
$ pidof perl | wc -l
没有$()
您最有可能遇到的问题是 cron 在路径中找不到 pidof。因此,您需要找出 pidof 驻留在系统上的位置:
$ which pidof
然后将该完整路径放入您的 cron 作业中,它应该可以工作。
关于linux - bash 脚本未按 cron 与 shell 的预期运行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27699075/