LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-04-2009, 07:37 AM   #1
jayarajmohan
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Rep: Reputation: 0
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
 
Old 11-04-2009, 08:00 AM   #2
avijitp
Member
 
Registered: May 2005
Location: India
Distribution: FC11, Debian/Ubuntu, RHEL, Solaris, AIX, HP-UX
Posts: 161

Rep: Reputation: 32
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.
 
Old 11-04-2009, 08:07 AM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
man crontab: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab
man 5 crontab: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
 
Old 11-04-2009, 02:00 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,098

Rep: Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450
Quote:
Originally Posted by jayarajmohan View Post
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.
 
Old 11-05-2009, 06:16 AM   #5
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
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..

Last edited by WhisperiN; 11-05-2009 at 07:10 AM.
 
Old 02-12-2016, 02:52 AM   #6
sebastjanp
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Thumbs up Great tut :)

Thanks guys easy and understandable.
 
Old 02-16-2016, 10:57 PM   #7
eklavya
Member
 
Registered: Mar 2013
Posts: 633

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by WhisperiN View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cupsd and httpd restarting every Sunday at 4:02 kmasaryk Red Hat 1 06-05-2008 04:14 PM
How do I restart X-Server? pxumsgdxpcvjm Fedora 4 12-21-2007 06:00 AM
Linux server running Ubuntu 6.06 freezing after a week or so hbbtstar Linux - General 5 02-06-2007 01:49 PM
Set up my first server last week. Now it is not working adrian29uk Linux - Networking 4 06-22-2005 03:44 AM
Restart X server jbraum Linux - General 2 02-09-2004 01:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration