Linux Crontab executes script task commands regularly & sets configuration file usage

Linux 's built-in cron process can help us achieve our needs for executing scheduled tasks. By using cron and shell scripts, we can easily execute very complex task commands on a schedule.

What is Cron?

We often use the crontab command, which is short for cron table.

It is the configuration file for cron, which can also be called the job list.

We can find the relevant configuration files in the following folders.

  • The /var/spool/cron/ directory stores crontab tasks for each user including root, and each task is named after the creator
  • /etc/crontab This file is responsible for scheduling various administrative and maintenance tasks.
  • /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.
  • We can also put scripts in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directories, let it execute every hour/day/week, month.

How is Crontab used?

Our commonly used commands are as follows:

crontab [-u username]    //省略用户名表示操作当前用户的crontab
    -e      (编辑工作表)
    -l      (列出工作表里的命令)
    -r      (删除工作表)

we usecrontab -eEnter the worksheet editing of the current user, which is a common vim interface.Each line is a command.

Edit Worksheet▼

crontab -e

List worksheets▼

crontab -l

Delete worksheet ▼

crontab -r 

crontab commands are structured as time + action. Time options include minutes, hours, day, month, and Friday. Operators include...

  • * all numbers in the range
  • / how many numbers
  •  from X to Z
  • .hash numbers

Crontab Execute Scheduled Task Command Example

Linux Crontab executes scheduled task commands and sets the configuration file usage

Example 1: Execute myCommand every 1 minute

* * * * * myCommand

Example 2: Execution on the 3rd and 15th minutes of every hour

3,15 * * * * myCommand

实例3:在上午8点到11点的第3和第15分钟执行

3,15 8-11 * * * myCommand

实例4:每隔两天的上午8点到11点的第3和第15分钟执行

3,15 8-11 */2  *  * myCommand

实例5:每周一上午8点到11点的第3和第15分钟执行

3,15 8-11 * * 1 myCommand

Example 6: Restart smb at 21:30 every night

30 21 * * * /etc/init.d/smb restart

实例7:每月1、10、22日的4 : 45重启smb

45 4 1,10,22 * * /etc/init.d/smb restart

Example 8: Restart smb at 1:10 every Saturday and Sunday

10 1 * * 6,0 /etc/init.d/smb restart

Example 9: Restart smb every 18 minutes between 00:23 and 00:30 every day

0,30 18-23 * * * /etc/init.d/smb restart

Example 10: Restart smb every Saturday at 11:00 pm

0 23 * * 6 /etc/init.d/smb restart

Example 11: Restart smb every hour

* */1 * * * /etc/init.d/smb restart

Example 12: Restart smb every hour between 11pm and 7am

* 23-7/1 * * * /etc/init.d/smb restart

How to delete specified Crontab task?

SSH enter the following crontab command ▼

crontab -e
  • Assuming multiple tasks, delete the specified scheduled task in vim (move the cursor to the configuration line to be deleted, press the delete key to delete)

:wq save and exit

Check to see if the Crontab task was deleted?

crontab -l
  • It is found that the crontab scheduled task that has just been deleted does not exist, which means that the deletion is successful.

How to restart the Cron service?

Using system service management commands : In some cases, you may need to use system service management commands directly to restart the cron service.

In particular, when adding or modifying Cron scheduled tasks in HestiaCP , the Cron service must be restarted for the changes to take effect.

This usually involves one of the following commands:

  • For use systemd System:

    sudo systemctl restart cron
    
  • For use init.d Old system for scripts:

    sudo /etc/init.d/cron restart
    
  • For use service Command system:

    sudo service cron restart
    

CWP Control PanelHow to Set Crontab Scheduled Tasks

  • In the scheduled task, add a synchronization command to automatically synchronize the backup files of the CWP control panel to GDrive.

If using the CWP Control Panel, log into the CWP Control PanelOf Server SettingCrontab for root ▼

How to set the Crontab timed task to automatically sync to GDrive in the CWP control panel?

In "Add Full Custom Cron Jobs", enter the following fully custom cron command ▼

00 7 * * * rclone sync /backup2 gdrive:cwp-backup2
55 7 * * * rclone sync /newbackup gdrive:cwp-newbackup
  • (Automatically sync local directory every morning at 7:00 am /backup2to the network disk with the configuration name gdrivebackup2Table of contents)
  • (Automatically sync local directory every morning at 7:55 am /newbackup  to the network disk with the configuration name gdrivecwp-newbackupTable of contents)
  • SynchronizeWordpressFor website files, it is recommended not to back up incrementally, because the test found that if the file names are the same, but the contents of the files are different, they will not be synchronized.

Because the rclone process may continue to run in the background after the scheduled start of automatic synchronization, it can consume up to 20% of CPU resources, resulting in a waste of server resources.

Therefore, it is necessary to add a fully customized scheduled task command to force the rclone process to close ▼

00 09 * * * killall rclone
  • (Automatically forcibly close the rclone process at 7:00 every morning)

If the CWP control panel cannot edit Crontab scheduled tasks, you can use FTPsoftwareopen a file /var/spool/cron/ Edit Crontab timed tasks directly.

How to use rclone to back up a VPS? Please click the link below to view the CentOS tutorial on automatic synchronization using GDrive ▼

Comment

Your email address will not be published. Required fields are marked with * .

Scroll to Top