Set a Cron Job in Linux

  There are occasions when you want to create a schedule tasks on your server.

  Suppose you want to backup your hard drive once a week, or you want to run a script at 6 am every morning.

  All those things that you want to run at specific intervals can be set with a cron job.

  The cron daemon is a long-running process that executes commands at user-specified intervals.

  This how-to provides a step-by-step tutorial on how you can schedule such a task using a program named crontab.

  

  Setting up crontab is very easy. To edit your crontab file use the following command

  crontab -e

  If default editor is not defined then you will see an error message:

  /bin/sh: /bin/vi: No such file or directory OR crontab: "/bin/vi" exited with status 12

  So do the command:

  export EDITOR=vim

 

  Now, vim will be used as the default editor. You may use any editor of your choice here (nano etc.).

  The pattern is easy:

  * * * * command/tobe/executed

  | | | | |

  | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)

  | | | ------- Month (1 - 12)

  | | --------- Day of month (1 - 31)

  | ----------- Hour (0 - 23)

  ------------- Minute (0 - 59)

  

  Suppose you want to run a shell script every hour

  0 * * * * /root/script.sh

  Run a command every 10 minutes

  */10 * * * * command

  Run a command at 5 am in Morning

  0 5 * * * command

  Run a command every weekend 2am

  0 2 * * 5-7 command

  Run a command every Saturday

  0 2 * * sat command

 

  Disable Email Output

  By default crontab returns emails to the root with the output of the cron, you might want to avoid it.

  Just pipe all the output to the null device, also known as the black hole. On Unix-like operating systems,/dev/null is a special file that discards all data written to it.

  0 * * * * /root/script.sh > /dev/null 2>&1

  You can define a MAILTO variable to output the mail to a particular email address.

  MAILTO="test@example.com"

  But this MAILTO will output the result of all the cronjobs. Suppose you want to get the result of 1 cronjob. You can do it using mailx. Make sure mailx is installed.

  yum install mailx                 #For Fedora/Centos

  apt-get install mailx    #For Ubuntu

  */10 * * * * /root/script.sh 2>&1 | mail -s "Output From Cron Job" username@example.com

  Erase all the Crontab jobs by using

  crontab -r

  Cronjob provide eight special strings that can also be used to make the file look more readable

  @reboot   Run at startup

  @hourly    Run at "0  1 1 * *" 

  @daily       Run at "0 0 * * *"  

  @midnight Run at "0 0 * * *" 

  @weekly    Run at "0 0 * * 0"

  @monthy   Run at "0 0 1 * *"

  @yearly     Run at "0 0 1 1 *"

  @annually Run at "0 0 1 1 *"

  • Cronjob
  • 6 A felhasználók hasznosnak találták ezt
Hasznosnak találta ezt a választ?

Kapcsolódó cikkek

Installing Multicraft

  Installing Dependencies   Step 1: You first need to install PHP, on Ubuntu, to do that please...

How to Secure Your Server

A Virtual Private Server (VPS) offers freedom, control, and resources beyond that of a shared...

Troubleshooting Common Issues

Managing a Virtual Private Server (VPS) isn't always smooth sailing – you might occasionally run...

Managing Processes

In a Linux environment, understanding and managing processes is essential for...

Using MTR to Debug Latency or Packet Loss Issues

Network issues can often lead to frustrating user experiences. Latency and packet loss are two...