简体   繁体   English

脚本在crontab中不起作用,但在终端中起作用

[英]Script doesn't work in crontab but works in the terminal

I'm attempting to run a bash script in crontab on Centos 5.5 but i don't see it running as a pid (pid -e). 我正在尝试在Centos 5.5上的crontab中运行bash脚本,但我看不到它作为pid(pid -e)运行。 The script itself uses an awk command to monitor a running log for a keyword then sends an email, then once the server is back online it sends a confirmation email: 脚本本身使用awk命令监视正在运行的日志中的关键字,然后发送电子邮件,然后服务器重新联机后,它将发送确认电子邮件:

tail -fn0 user | awk '
    !server_down && /disconnect_tcp_conn/ { 
        system("mail -s \"server down\" mail@gmail.com </dev/null") 
        print "server down"
        server_down = 1 }
    server_down && /daemon apps started/ {
        system("mail -s \"server's back!\" mail@gmail.com </dev/null") 
        print "Server's back!"
        server_down = 0 }' 

Originally, I had trouble with running the script while the terminal was closed. 最初,我在终端关闭时无法运行脚本。 Someone suggested using setsid in the following way: 有人建议以以下方式使用setsid:

setsid server_mon.sh >/dev/null 2>&1 < /dev/null &

And this works great.. until it doesn't. 这很有效..直到没有。 For some reason, after some time like a day or so, the script no longer sends notifications. 由于某种原因,经过一天左右的时间后,脚本不再发送通知。 So, I thought to create a cronjob for the script. 因此,我想为该脚本创建一个cronjob。

Not being familiar with bash shell and not much of a programmer i tested the script in cron every minute to see if i was using it right: 我不熟悉bash shell,也没有太多的程序员,所以我每分钟都在cron中测试该脚本,以查看我是否正确使用了它:

* * * * *  /var/log/server_mon.sh

Didn't work. 没用 It would only say: 它只会说:

"/tmp/crontab.XXXXmu99ii" 19L, 760C written
crontab: installing new crontab

Tried to use rc.local.. can't. 试图使用rc.local ..不能。 I think the centos version is too old (5.5), plus, the crontab might be the way to go because i might want to find out at what point the notifications seems to stop working and set up a cron job accordingly.. at least until i have time to fix it. 我认为centos版本太旧(5.5),此外,crontab可能是一种解决方法,因为我可能想找出通知似乎在什么时候停止工作并相应地设置cron作业..至少直到我有时间修复它。

tried adding the path to the awk command in the script: /usr/bin/awk Still no pid. 尝试在脚本中将路径添加到awk命令:/ usr / bin / awk仍然没有pid。

tried adding the following changes from what i found on the web: 尝试根据我在网上发现的内容添加以下更改:

setsid bash -lic 'cd /var/log && exec SLR_reaper.sh -start' </dev/null &>/dev/null &

didn't work, and not sure if it's right. 没用,也不确定是否正确。

my expected result is to get this: 我的预期结果是得到这个:

setsid server_mon.sh >/dev/null 2>&1 < /dev/null &

somehow to be triggered by crontab: 由crontab触发:

@reboot /var/log/server_mon.sh

       *or*

* * * * * /var/log/server_mon.sh

Just to see it work. 只是看到它的工作。

Any direction or help would be great. 任何方向或帮助都会很棒。 If you need more info please let me know also. 如果您需要更多信息,请让我知道。 Thanks! 谢谢!

A screen shot of the log sent to mail: enter image description here 发送到邮件的日志的屏幕截图: 在此处输入图像描述

So the solution was pretty simple. 因此,解决方案非常简单。 I just had to provide the path to the file the script was monitoring, in the script itself: 我只需要在脚本本身中提供脚本正在监视的文件的路径:

tail -fn0 /var/log/user | /usr/bin/awk '
    !server_down && /disconnect_tcp_conn/ {
        system("mail -s \"server is down XXX.XXX.XXX.XXX\" mail@gmail.com </dev/null")
        print "server is Down XXX.XXX.XXX.XXX"
        server_down = 1 }
    server_down && /daemon apps started/ {
        system("mail -s \"Server is up XXX.XXX.XXX.XXX\" mail@gmail.com </dev/null")
        print "Server is Up XXX.XXX.XXX.XXX"
        server_down = 0 }'

The log file reported that it couldn't find the file which clued me to it being a path related issue. 该日志文件报告找不到与路径相关的问题,这使我无法理解。

Previously the script read: 以前的脚本为:

tail -fn0 user | /usr/bin/awk '

Now I added the full path: 现在,我添加了完整路径:

tail -fn0 /var/log/user | /usr/bin/awk '

Kudos to @EdMorton for telling me to track the log and then provide the full path in the script. 感谢@EdMorton告诉我跟踪日志,然后在脚本中提供完整路径。 I didn't realize this was necessary because the script was in the same directory as "user". 我没有意识到这是必要的,因为脚本与“用户”位于同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM