In Linux and Unix systems, you can schedule any command or program to run at definite intervals (or at pre-defined times) using the job scheduler framework. The most popular job-scheduling utility is cron, and the jobs run are commonly referred to as the cron jobs.
Sometimes, you want to run the job only for a small amount of time (or just for the current session) and you don’t want to mess around with the cron. So, you need a quick way to run a command repeatedly at definite intervals. This method is also useful if you do not have the system privileges to create and run cron jobs. You should also be able to stop the command as well from the command line.
We will see how you can quickly execute a recurring job from the command line. We will also create a simple shell script from this which will allow you easily execute it in the future. The shell statement that you want to use is while. The while is a shell statement that can be used to execute a particular set of commands till the condition is false.
The generic syntax of the while loop as a one-liner is:
while [condition]; do [command 1]; [command 2]; done
Let’s say we want to find who is currently logged into the machine. This is just an example, so don’t worry too much about the functionality of it. The easy way to find users that are currently logged into the machine is by using the who command. We will use the following command an save the output to a file every 30 seconds.
$ who -a
In order to schedule this command to be run every 30 secs, we can use this one liner.
$ while true; do who -a >> who-output.txt; sleep 30; done &
Most of the above command should be self-explanatory. We will go through it anyway just to see what exactly is happening.
- The while loop condition is set to true, which means it will loop infinitely because the condition is always true.
- The command that is executed is who -a and the output of the command is appended to a file named who-output.txt using the redirection operator ( >> ).
- The next command in the chain is sleep which takes a numeric value in seconds. The sleep 30 puts the loop in sleep or suspends it for 30 seconds.
- The ampersand (&) puts the entire command sequence to be executed in the background. This is one way of executing commands or jobs in the background in Linux.
Now, this will run until you explicitly kill it or the machine reboots. Let’s see how we can stop the command when we want to. There are a couple of different ways to do it.
If the command window where you executed the command is still open, then you can bring the background job to the foreground by using the fg command and then use Ctrl-C keyboard shortcut to kill that process. The set of commands that you execute will look like the example shown below.
$ fg [job number]
It is possible that you probably do not have the command window open. In that case, you will need to kill the running process using the kill command and the process id.
In the above example, we used a simple one line command (who) as an example. You can also execute multiple commands or execute a shell script in the same fashion. An example of executing a shell script
$ while true; do ~/bin/mycustomscript.sh; sleep 60; done &
Now, if you have to execute some thing similar quite often, it can get quite wordy to type it out every time. It is quite easy to create a simple shell script to do this and accept the command as an argument. A rudimentary script that will do the above task…
Create a script file called myc.sh (eg. short for “my cron”). You can call it anything you want. And put the following code in the file. Remember to make the script executable as well.
if [ $# -eq 0 ]
echo "Usage: myc.sh [command] [output file] [seconds]"
while true; do $1 >> $2; sleep $3; done
Now, you can execute this script easily from command line with different parameters such as:
$ myc.sh who output.txt 30
$ myc.sh /home/tom/bin/script.sh ~/temp/output.txt 60
You can modify the script to include additional checks for the input parameter, accept advanced commands (with arguments) or multiple commands etc etc. I will leave those as an assignment to you :-).
For the sake brevity and completeness, you should check out the linux command called watch. This command allows you to execute a program periodically and display its output full screen. It is quite possible that this command will suffice for your purposes.