Advertisement
efxtv

How to Automate Tasks with cron Jobs in Linux

Oct 2nd, 2023 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | Cybersecurity | 0 0
  1. Topic: How to Automate Tasks with cron Jobs in Linux (on the basis of time,date,month,year, etc)
  2.  
  3. Q. What is CRON JOB?
  4. A. The cron command-line utility is a job scheduler on Unix-like operating systems. Users who set up and maintain software environments use cron to schedule jobs, also known as cron jobs, to run periodically at fixed times, dates, or intervals.
  5.  
  6. # Cron Jobs are stored in a location
  7. ls /etc/cron #Press tab two times to list all the files
  8.  
  9. # Restart cron jobs
  10. sudo systemctl status cron.service
  11.  
  12. # Cron job syntax
  13. crontab -e: edits crontab entries to add, delete, or edit cron jobs.
  14. crontab -l: list all the cron jobs for the current user.
  15. crontab -u username -l: list another user's crons.
  16. crontab -u username -e: edit another user's crons
  17.  
  18. # List crons
  19. $ cron job example
  20.  
  21. # What do **** mean
  22. * * * * * represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.
  23.  
  24. * * * * * sh /path/to/script/script.sh
  25. | | | | | |
  26. | | | | | Command or Script to Execute
  27. | | | | |
  28. | | | | |
  29. | | | | |
  30. | | | | Day of the Week(0-6)
  31. | | | |
  32. | | | Month of the Year(1-12)
  33. | | |
  34. | | Day of the Month(1-31)
  35. | |
  36. | Hour(0-23)
  37. |
  38. Min(0-59)
  39.  
  40. # To list or check the running task use the command.
  41. $ crontab -l
  42.  
  43. # To establish the task
  44. $ crontab -e #choose nano and hit enter
  45. * * * * * (5 start denoted by abcde for explaining)
  46. # a b c d e command
  47. # a=minutes (00,..60)
  48. # b=hour (01,02..12)
  49. # c=date (01,02..10,11..30,31)
  50. # d=month (jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec)
  51. # e=Day (sun,mon,tue,wed,thu,fri,sat)
  52.  
  53. # schadule the task
  54. 11 02 * * * touch /user/Desktop/file.txt
  55.  
  56. # It will create a file every day at 2 a.m. for 11 minutes. If you fill rest of the three dots you can set a specific date month and day as well.
  57.  
  58. # To stop the task you can use the command
  59. $ crontab -r #close all schedules without any notification
  60. $ crontab -i -r #close schedules with notifications
  61.  
  62. # Create user-specific task
  63. $ crontab -u username -e #now again fill the box as shown below
  64. 11 12 * * * mkdir cool #it will create the folder every day for 11 minutes and 12 pm
  65.  
  66. #repeat task after every 5 hours
  67. 0 */5 * * * touch /usr/bin/script
  68.  
  69. #Repeat the task after every 30 minutes
  70. */30 * * * * command
  71.  
  72. #Repeat multiple tasks after 30 minutes
  73. */30 * * * * command; command
  74.  
  75. Using special strings to save time on writing cron jobs
  76. 1) @hourly timestamp corresponds to 0 * * * *
  77. It will execute a task in the first minute of every hour.
  78. @hourly /path/to/script
  79.  
  80. 2) @daily timestamp is equivalent to 0 0 * * *
  81. It executes a task in the first minute of every day (midnight). It comes in handy when executing daily jobs.
  82. @daily /path/to/script
  83.  
  84. 3) @weekly timestamp is the equivalent to 0 0 1 * mon
  85. It executes a cron job in the first minute of every week, whereby a week starts on Monday.
  86. @weekly /path/to/script
  87.  
  88. 3) @monthly is similar to the entry 0 0 1 * *
  89. It carries out a task in the first minute of the first day of the month.
  90. @monthly /path/to/script
  91.  
  92. 4) @yearly corresponds to 0 0 1 1 *
  93. It executes a task in the first minute of every year and is useful in sending New Year greetings ๐Ÿ™‚
  94. @monthly /path/to/script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement