Scheduling a job/command in Linux is acronym to cron. Whenever we need to schedule a job, we call cron, but do you know we can schedule a job at a later time without corn? You can do it as suggested below.
Code:
$ while true; do date >> date.txt ; sleep 5 ; done &
- while true Ask script to run while the condition is true, it acts as a loop which makes the command to run again-and-again or say in a loop.
- do do perform what follows, ie., execute command or set of commands that lies ahead of do statement.
- date >> date.txt here the output of date command is being written to a file date.txt. Also note that we have used >> and not >.
- >> ensures that the file (date.txt) is not overwritten every time the script execute. It just append the changes. Whereas > overwrite the file again and again.
- sleep 5 It ask the shell to keep a time difference of 5 seconds before it executed again. Note the time here is always measured in seconds. Say if you want to execute the command every 6 minutes, you should use (6*60) 360, in succession of sleep.
- done marks the end of while loop.
- & Put the whole process in loop to background.
Also worth mentioning that the script above should be run in the directory where the script to be called lies, else you need to provide full path (/home/$USER//script_name.sh). The syntax for calling script at above described interval is:
Code:
$ while true; do /bin/sh script_name.sh ; sleep 100 ; done &
Comment