LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   I want to restart my server every week sunday (https://www.linuxquestions.org/questions/linux-server-73/i-want-to-restart-my-server-every-week-sunday-766784/)

jayarajmohan 11-04-2009 07:37 AM

I want to restart my server every week sunday
 
Hi all,

I want to set a crontab job for restarting my linux machine every sunday ..

How to do this ?

JayaraJMohan.J

avijitp 11-04-2009 08:00 AM

As root user:

Code:

crontab -e
then add following :

Code:

30 1  *    *    7 /sbin/shutdown -r now
This will reboot your server at 1:30 AM server time.

rjlee 11-04-2009 08:07 AM

man crontab: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab
man 5 crontab: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5

TB0ne 11-04-2009 02:00 PM

Quote:

Originally Posted by jayarajmohan (Post 3744128)
Hi all,

I want to set a crontab job for restarting my linux machine every sunday ..

How to do this ?

You write a script that reboots the box, and put it in CRON to run every Sunday.

WhisperiN 11-05-2009 06:16 AM

Alright,

I'll assume, that you want to reboot the system whenever it reaches seven days uptime..
Keep on with me, and follow the instructions..

First, we will create a new script, shell script, run:

Code:

nano /bin/RestartMe
Then, copy and paste the following (Copy from here and then right click into the Putty's window to paste it):

Code:

#!/bin/bash

up=$(/usr/bin/uptime | cut -c14-19)

if [[ "$up" = "7 days" ]]; then
/sbin/shutdown -r +2
fi

exit 0

# END

And, then close and save the file

Code:

CTRL + X then Hit Y and then ENTER
Change the file mode to executable:

Code:

chmod 755 /bin/RestartMe
Now, we are done with the reboot script, we will setup a cronjob to run every night:

To open crontab editor:

Code:

crontab -e
and then at the end type the followin:

Code:

30 2 * * * /bin/RestartMe

Summary:
The crontab we sat above, will run the script every night at 2:30 AM server time, thus the script will check for the server uptime, if it's 7 days, a system reboot will be fired.
The reboot command will wait 2 minutes before doing the real reboot, this way it gives a chance for any logged user to finish any running work.

Otherwise, if you want to reboot the system on Sunday after 2 after midnigh (Monday Early Morning), no matter how long is the uptime.. go like:

Code:

crontab -e
At the end type:

Code:

30 2 * * 1 /bin/RestartMe
Hope we helped.. :-)
Regards..

sebastjanp 02-12-2016 02:52 AM

Great tut :)
 
Thanks guys easy and understandable.

eklavya 02-16-2016 10:57 PM

Quote:

Originally Posted by WhisperiN (Post 3745555)
Alright,

I'll assume, that you want to reboot the system whenever it reaches seven days uptime..
Keep on with me, and follow the instructions..

First, we will create a new script, shell script, run:

Code:

nano /bin/RestartMe
Then, copy and paste the following (Copy from here and then right click into the Putty's window to paste it):

Code:

#!/bin/bash

up=$(/usr/bin/uptime | cut -c14-19)

if [[ "$up" = "7 days" ]]; then
/sbin/shutdown -r +2
fi

exit 0

# END

And, then close and save the file

Code:

CTRL + X then Hit Y and then ENTER
Change the file mode to executable:

Code:

chmod 755 /bin/RestartMe
Now, we are done with the reboot script, we will setup a cronjob to run every night:

To open crontab editor:

Code:

crontab -e
and then at the end type the followin:

Code:

30 2 * * * /bin/RestartMe

Summary:
The crontab we sat above, will run the script every night at 2:30 AM server time, thus the script will check for the server uptime, if it's 7 days, a system reboot will be fired.
The reboot command will wait 2 minutes before doing the real reboot, this way it gives a chance for any logged user to finish any running work.

Otherwise, if you want to reboot the system on Sunday after 2 after midnigh (Monday Early Morning), no matter how long is the uptime.. go like:

Code:

crontab -e
At the end type:

Code:

30 2 * * 1 /bin/RestartMe
Hope we helped.. :-)
Regards..

If you count uptime of the server, it is not necessary that it will shutdown the system on every Sunday. Somehow if server has been restarted between the week, in this case it will not restart it on Sunday.
If OP wants server should be restarted on sunday then I think a simple cronjob with shutdown -r now or init 0 command will do the task.


All times are GMT -5. The time now is 06:18 AM.