[Guide] How to make autorestart using CRON [Debian/Ubuntu]

Recommended Posts

So this guide will help to make autorestart of MTA Server on linux Debian or Ubuntu (a daily restart improves server stability/minimizes lag). This works on all servers, so you need program called "CRON" to make autorestart. The program utility Cron is a time-based job scheduler in Unix-like computer operating systems. 

№1 Check to see if "Cron" is already installed.

which cron

№2  If a path is returned, example: "/usr/bin/cron", then Cron is installed. Skip to №4.
№3 Install Cron by typing.

apt-get install cron

№4 Now that Cron is installed, check the time line of crontab. Each line of a crontab file represents a job, and looks like this:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
│ │ │ │ │                                       7 is also Sunday on some systems)
│ │ │ │ │
│ │ │ │ │
* * * * *  command to execute

The syntax of each line expects a cron expression made of five fields, followed by a shell command to execute.
For example i'll make autorestart every 24 hours. at time 03:00 By moscow time, before restart he will warn in 1 minutes about.  (You can set your time zone by typing: dpkg-reconfigure tzdata)

Create new crontab file (requirements nano, if you don't have install by typing apt-get install nano)

crontab -e

Send this example code using nano:

59 2 * * * screen -dr <NAME_OF_YOUR_SCREEN_WHERE_STARTED_MTA_SERVER> -p 0 -X stuff "$(printf "say Server will restart in 1 minute\r")"
00 3 * * * screen -dr <NAME_OF_YOUR_SCREEN_WHERE_STARTED_MTA_SERVER> -p 0 -X stuff "$(printf "shutdown\r")"
01 3 * * * screen -dr <NAME_OF_YOUR_SCREEN_WHERE_STARTED_MTA_SERVER> -p 0 -X stuff "$(printf "./mta-server\r")"


Now save file with ctrl+x click y then click enter. 

File created, so all files crontab which makes commands by time located in /var/spool/cron/crontabs

№5 Restart cron service by typing 

service cron restart

Now MTA Server Should to make autorestart every 24 hours at 03:00 Moscow time.

If you have problems with this, send to this thread your problem and i will reply.

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.