Creating file using crontab


Last Blog Review →
In the last blog we understood, how we understand the cronjob format command to be used in the different scenarios. The combination on how the cronjob can be used to execute the script. Also we some great examples which are highly useful..
Lets create a file using crontab.
I have logged in ubuntu server. It has the cron already installed in it..
Lets check the date - using "date" command
i-0aba63c0d848c67c7 (test-1) ---------------------------- ubuntu@ip-172-31-21-169:~$ date Fri Sep 6 16:59:09 UTC 2024 ubuntu@ip-172-31-21-169:~$
Now lets create a file 4 mins later i.e. at 17:04 PM using crontab -e. So, it will ask for editor which you want. So, I go with the vim.basic
i-0aba63c0d848c67c7 (test-1) ---------------------------- ubuntu@ip-172-31-21-169:~$ crontab -e no crontab for ubuntu - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/nano <---- easiest 2. /usr/bin/vim.basic 3. /usr/bin/vim.tiny 4. /bin/ed Choose 1-4 [1]: 2 crontab: installing new crontab ubuntu@ip-172-31-21-169:~$
Now lets add the cron command "4 17 * touch text.txt" which means lets create a text.txt file at 17:04 PM.
i-0aba63c0d848c67c7 (test-1) ---------------------------- ubuntu@ip-172-31-21-169:~$ crontab -l # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any'). # # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command 4 17 * * * touch text.txt ubuntu@ip-172-31-21-169:~$
Now lets check the date using command "date". As we see its still 17:03 and we verified if the text.txt file is not created still using "ls"
i-0aba63c0d848c67c7 (test-1) ---------------------------- ubuntu@ip-172-31-21-169:~$ date Fri Sep 6 17:03:00 UTC 2024 ubuntu@ip-172-31-21-169:~$ ls ubuntu@ip-172-31-21-169:~$
Now lets check the date and time again. Now its 17:04, so our file should be created. So, lets verify using "ls". Yes we have the "text.txt" file created.
i-0aba63c0d848c67c7 (test-1) ---------------------------- ubuntu@ip-172-31-21-169:~$ date Fri Sep 6 17:04:03 UTC 2024 ubuntu@ip-172-31-21-169:~$ ls text.txt ubuntu@ip-172-31-21-169:~$
Conclusion →
In this blog we understood, how simple it is to use crontab commands and how the cron schedules and creates the file at the required date and time, this simplifies the task to be done without manually being present in the moment and do it helps to do time management.
Image credit → https://www.tutorialspoint.com/unix_commands/images/edit_crontab_file.jpg
Subscribe to my newsletter
Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mihir Suratwala
Mihir Suratwala
Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.