In: Computer Science
Create a cronjob which does the following tasks
1.) create a file named <yourname>.txt
2.) in the 35th minute of the hour change the permission to 755
3.) Create a shell script named first.sh in which you print your name and redirect the output by 45th minute of the hour to the text file
We will try to complete the tasks one by one.
1. Create a file named <yourname>.txt. My name is Mohit so I will create mohit.txt. I will create this file in a new task folder in my home directory. You can choose the file location anywhere you want. Command for creating a new file - touch mohit.txt
2. Now we need to change the permission of this file by using corn job. Since permission can be changed by root users, we will edit the /etc/crontab file and add the cronjob there as a root user. We need to change the permission of first.sh so that it becomes executable.
The cron command to run the permission change is-
35 * * * * * root /usr/bin/chmod 755 /home/mohit/task/first.sh
After adding this line to /etc/crontab using - sudo nano /etc/crontab it looks like-
3. Create the shell script in the same task directory by using touch command.touch first.sh
The contents of the script will be-
echo "Mohit"
Either you can redirect the output of this file to text file here or in the cron job. I will do that in the cron job which is a better usage of cron job.
The cron script for this command to be executed at 45th minute will be-
45 * * * * * <username> /home/mohit/task/first.sh -> /home/mohit/task/mohit.txt
Replace <username> with your terminal username. In the screenshots you can see my username is 'mohit'
The updated rontab file will look like-
After this taks has run the contents of file mohit.txt will change and will look lke-