Configuring CentOS to reboot from CRON

Installing, Configuring, Troubleshooting server daemons such as Web and Mail
Post Reply
gwelmarten
Posts: 4
Joined: 2013/01/16 11:13:15
Contact:

Configuring CentOS to reboot from CRON

Post by gwelmarten » 2013/01/16 11:19:02

Hi

I'm relatively new to CentOS and Linux. I have done some CRON stuff before though.

What I am trying to achieve is to make my server reboot from a CRON job every other day. I log into the root account and try the following:

1). Configuring crontab -e by adding the following line: 0 0 */2 * * shutdown -r now
This does not work.
2). As above, but with 0 0 */2 * * reboot
3). Putting files into /etc/cron.daily on the server. These include my_reboot (created by 'nano my_reboot') and my_reboot.sh, and one set contain the command reboot and the other contain shutdown -r now.

What am I doing wrong? How can I get this to work? Should I be somehow saying to run the command as wheel group user? Or am I using the command wrongly? Can somebody explain to me how to get this to work?

Thanks,

Sam

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Configuring CentOS to reboot from CRON

Post by TrevorH » 2013/01/16 11:53:21

Cron has a very limited set of environment variables, including PATH. It's usually best to include the full path to the thing you want to run - i.e. /sbin/shutdown not just shutdown.

However, having said that, Linux is not Windows and shouldn't need to be rebooted every two days so perhaps you'd rather share the actual problem you're trying to solve?

gwelmarten
Posts: 4
Joined: 2013/01/16 11:13:15
Contact:

Re: Configuring CentOS to reboot from CRON

Post by gwelmarten » 2013/01/16 14:29:50

Hi

Thanks for your reply. I'll add it and schedule it for tonight - hopefully it will work great!

To be honest I've given up on solving the problem. My server is running CentOS 5. The problem is that I've got some problem with a program with memory management. I host it for my work since sometimes we are sent to countries with controlled internet access. The server runs Glype Proxy (an old version), which is written in PHP.

I am aware that Linux should be able to be left on for months on end, but in this case I have spent some time trying to fix the problem and have been unable to. Rebooting seemed like the best option - doing it every second or third day, gives me 2 minutes downtime for 3 days uptime - seems fine to me.

Have you got any other ideas about how I can fix this? I thought it might be a problem with the proxy script originally, but since it's PHP I don't think that could be the cause (even though it handles about 800gb of traffic a month). The server has WHM/cPanel and runs with 4GB Ram.

Thanks in advance for any help,

Sam

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Configuring CentOS to reboot from CRON

Post by TrevorH » 2013/01/16 20:59:54

If it's something which is started as a service out of /etc/init.d then just restarting that service should help I'd think. Most likely explanation is that php is configured to use too much memory and/or the application has a memory leak and eats RAM. Scheduling a `/sbin/service whatever restart` every couple of days might help and give less downtime than a full reboot. If it doesn't have its own service then perhaps it runs under apache in which case `service httpd restart` might do the same thing.

jaxx0rr
Posts: 1
Joined: 2014/08/09 00:46:53

Re: Configuring CentOS to reboot from CRON

Post by jaxx0rr » 2014/08/09 00:51:01

Can you stop answering a question with another question ?
Me and I'm sure hundreds of others also want to know if you can reboot linux via cron !??
That's why forums are for. Its not about the OP but about the hundreds that search for help on google, come here, and face selfishness and misinformation.
Is it possible to restart centos from cron? Yes or no?

User avatar
avij
Retired Moderator
Posts: 3046
Joined: 2010/12/01 19:25:52
Location: Helsinki, Finland
Contact:

Re: Configuring CentOS to reboot from CRON

Post by avij » 2014/08/09 06:54:11

Yes, it is possible to reboot CentOS from cron. You may need to use the full path to the shutdown command to achieve that, as kindly pointed out by TrevorH in his first comment.

The reason why an alternative approach (restart the affected service) was offered was because it is usually a better solution than rebooting the server. I don't see why offering a better solution to someone's problem would be considered rude or selfish.

Post Reply