DEV Community

Cover image for Process Scheduling in Linux
Waji
Waji

Posted on

1

Process Scheduling in Linux

cron is a process scheduler for Linux. The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.

πŸ‘‰ cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run automatically at a specified time and date.

crontab is the program used to install, deinstall or list the tables used to drive the cron daemon. Each user can have their own crontab, and though these are files in /var, they are not intended to be edited directly.

πŸ’‘ Format
MIN HOUR DOM MON DOW CMD

Field    Description    Allowed Value

MIN      Minute field    0 to 59
HOUR     Hour field      0 to 23
DOM      Day of Month    1-31
MON      Month field     1-12
DOW      Day Of Week     0-6
CMD      Command         Any command to be executed
Enter fullscreen mode Exit fullscreen mode

β€œ * β€œ λͺ¨λ“  값을 의미 β€œ * ” : λͺ¨λ“  일 λ§ˆλ‹€ ( 3번 ν•„λ“œ )
β€œ - " λ²”μœ„ 지정 β€œ 1-12 ” : 1μ›” λΆ€ν„° 12μ›” ( 4번 ν•„λ“œ )
β€œ , β€œ μ—¬λŸ¬ 개의 κ°’ 지정 β€œ 10,15 ” : 10μ‹œ 그리고 15μ‹œ ( 2번 ν•„λ“œ )
β€œ / β€œ νŠΉμ • 주기둜 λ‚˜λˆŒ λ•Œ μ‚¬μš© β€œ */10 ” : 맀 10λΆ„ λ§ˆλ‹€ ( 1번 ν•„λ“œ )

  • Linux Systemμ—μ„œ 주기적인 μž‘μ—…μ²˜λ¦¬λ₯Ό 진행할 λ•Œ μ£Όλ‘œμ‚¬μš© λœλ‹€. ( μ˜ˆμ•½ μž‘μ—…μ„ 의미 )
  • Cron은 ν”„λ‘œμ„ΈμŠ€ μ˜ˆμ•½ 데λͺ¬μ΄λ©°, νŠΉμ •μ‹œκ°μ— 지정 된 μž‘μ—…μ„ μˆ˜ν–‰ν•œλ‹€.
  • Crontab은 Cron에 μ˜ν•΄ μ‹€ν–‰ 될 μ˜ˆμ•½ μž‘μ—…μ˜ λͺ©λ‘μ„ μ •μ˜ν•˜λŠ” νŒŒμΌμ„ λ§ν•œλ‹€. ( CronTable )
  • Cron은 μ‚¬μš©μžλ³„ μ˜ˆμ•½μž‘μ—…μ„ λ”°λ‘œ κ°€μ§ˆ 수 μžˆλ‹€
  • Cronμž‘μ—…μ— λŒ€ν•œ λ‘œκ·ΈκΈ°λ‘μ€ /var/log/cron에 μ €μž₯ λœλ‹€.
rpm -qa | grep cronie
Enter fullscreen mode Exit fullscreen mode
ps -ef | grep cron
Enter fullscreen mode Exit fullscreen mode
ls -l /var/log/cron
Enter fullscreen mode Exit fullscreen mode
ls -l /var/spool/cron
Enter fullscreen mode Exit fullscreen mode
ls -d /etc/cron
Enter fullscreen mode Exit fullscreen mode

Setting up cron

  • crontab -l : μ˜ˆμ•½ μž‘μ—… 리슀트 확인
  • crontab -e : μ˜ˆμ•½ μž‘μ—… νŽΈμ§‘
  • crontab -r : μ˜ˆμ•½ μž‘μ—… μ‚­μ œ
  • crontab -u [UserName] : νŠΉμ • μ‚¬μš©μžμ˜ μ˜ˆμ•½μž‘μ—… 확인 및 νŽΈμ§‘

πŸ’‘ We can schedule a task or a process using the crontab -e command within the vi editor. Also, only the β€˜root’ user can use the crontab -u [UserName] command.ca

We can also see how the crontab file looks like:

➜  ~ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
Enter fullscreen mode Exit fullscreen mode

Examples

Example1:

  • Making an empty directory with a script file in it. Providing β€˜execute’ permission to the file as well.

    ➜  ~ mkdir ./script
    ➜  ~ cd ./script
    
    ➜  script vi ./cron_test1.sh
    
    #!/bin/bash
    
    echo "Cron Test" > /root/cron.txt
    
    ➜  script chmod +x ./cron_test1.sh
    
  • Setting up the schedule for the script to run and confirming it.

    ➜  script crontab -e
    0 19 * * * /root/script/cron_test1.sh
    
    ➜  script crontab -l
    0 19 * * * /root/script/cron_test1.sh
    
  • After the script runs at 19:00

    ➜  script ls -l /root
    total 12
    -rw-r--r--   1 root root    0 Jan 12 15:12 풀이
    -rw-------.  1 root root 1483 Jan 10 11:41 anaconda-ks.cfg
    **-rw-r--r--   1 root root   10 Jan 26 23:24 cron.txt
    
    # Checking the logs for cron**
    ➜  script tail /var/log/cron
    

Example2:

  • Creating a new script that saves logs as tar backup file and deletes it after 10 days.

    ➜  script vi ./cron_test2.sh
    
    #!/bin/bash
    
    DATE=$(date +%Y-%m-%d)
    BACKUP_DIR="/backupβ€œ
    tar -cvzpf $BACKUP_DIR/test-$DATE.tar.gz /var/log 
    find $BACKUP_DIR/* -mtime +10 -exec rm {} \;
    
  • Giving the file executable permissions and also adding new crontab settings.

    ➜  script chmod +x ./cron_test2.sh
    ➜  script crontab -e
    crontab: installing new crontab
    
    0 20 * * * /root/script/cron_test2.sh
    
  • After the script runs at 20:00

    ➜  script ls -l /backup
    test-26-01-2023.tar.gz
    
    **# Checking the logs for cron**
    ➜  script tail /var/log/cron
    

Deletion, Backup and Control

  • Before deleting our crontab settings,

    ➜  ~ crontab -l > /root/cron_back.txt
    ➜  ~ cat /root/cron_back.txt 
    0 19 * * * /root/script/cron_test1.sh
    0 20 * * * /root/script/cron_test2.sh
    
  • Deleting the crontab settings

    ➜  ~ crontab -r
    ➜  ~ crontab -l
    no crontab for root
    
  • We can easily backup our crontab settings using the bakcup .txt file that we created.

    ➜  ~ crontab /root/cron_back.txt
    ➜  ~ crontab -l
    0 19 * * * /root/script/cron_test1.sh
    0 20 * * * /root/script/cron_test2.sh
    
    πŸ‘‰ Just a note that we cannot delete a specific `crontab`
  • We can β€˜control’ what users can set up crontab configs

    ➜  ~ vi /etc/cron.deny
    itbank
    
    # If we try to use the following command as 'itbank'
    ➜ itbank ~ crontab -e
    You (itbank) are not allowed to use this program (crontab)
    See crontab(1) for more information
    

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more