restart service crontab problem

落花浮王杯 提交于 2020-01-05 07:23:30

问题


if i run the script below manually from putty (as root) then the service is restarted no problem

manual command

sh /home/auser/server/reService.sh >> /var/log/reService.log

But if the same script is scheduled as a cronjob, also under root:

crontab command

00 07 * * * /home/auser/server/reService.sh 2>&1 >> /var/log/reService.log

then the 'service myService restart' is not executed successfully: service is not restarted, and no command output in scheduled log compared to manual log. I can also tell you that nothing after the restart line executes if scheduled.

Why, am I missing some privileges when going through crontab? How can i successfully schedule restarting of myService?

thanks ahead.

reService.sh

#!/bin/bash
clear
echo "============================================================================="
echo "Daily restarting of my service"
date
echo "============================================================================="
service myService restart
exit 0

manual log

 [H [J=============================================================================
Daily restarting of my service
Thu May 19 04:51:12 EDT 2011
=============================================================================
Stopping myService   ..done
Starting myService   ..done

scheduled log

=============================================================================
Daily restarting of my service
Thu May 19 07:00:02 EDT 2011
============================================================================= 

回答1:


The use of sh in your manual example implies that the script doesn't have execute permission. Try setting that or calling sh in the same way within the crontab (I prefer the former).

EDIT: OK, it's not permissions. Next thing to look at is $PATH; echo that to the log file and compare them.

EDIT2: If PATH is the issue, then just set it within crontab:

PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin/:/usr/local/sbin:/usr/bin/X11:/usr/‌​openwin/bin:/usr/java/bin:/root/bin 

00 07 * * * /home/auser/server/reService.sh 2>&1 >> /var/log/reService.log


来源:https://stackoverflow.com/questions/6124699/restart-service-crontab-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!