In this section, we will show you how to automate Bash shell script execution. The cron system, which is available on every Linux system, allows for the automation of commands or scripts by enabling the administrator to determine a predefined schedule based on any hour, day, or even month. It is a standard component of the CentOS 7 operating system, and in this section we will introduce you to the concept of managing recurring tasks in order to take advantage of this invaluable tool.
First, let's create a new script, which will download an elegant and useful Linux command-line example from the incredible Commandlinefu web page and put it in the motd, or message of the day, file in the Linux system so that it is visible whenever a user logs into the system. The motd file is a simple text file from which the content will be displayed on successful login. We then run the script as a cron job so that the message of the day will be updated every day, which is very useful to learn a new and elegant command-line solution each day.
In order to do that, first log in as root, as the cron system is located in the system directories. Next, make a copy of the original motd file. Afterward, let's create our script file to update the motd file in the system:
#!/bin/bash Wget -0 /etc/motd http://www.commandlinefu.com/commands/random/plaintext
This script is normal batch script and downloads a random Commandlinefu example from the web page, http://www.commandlinefu.com/commands/random/plaintext, using the wget program and saves the downloaded file as /etc/motd. So, we can directly see the content when we log in to the system. Now, let's test drive our new script:
As you can see, the script has been successfully downloading a Commandlinefu from the http://www.commandlinefu.com/ web page. To test whether the Commandlinefu web page URL we use truly returns a random command-line example, let's restart our script:
As you can see, the command-line example is different this time. Now, based on your own preferred schedule of script execution, you need to decide how often you want to execute the script. There are some special cron directories in the filesystem for execution of system-wide cron jobs, and you can access them using # ls /etc/cron* -d. The folders are called cron.daily, cron.hourly, cron.weekly, and cron.monthly and are in the /etc directory, and their names refer to the time point that they are run. So, if we want our new Commandlinefu script to be started on a daily basis, just drop the script file into the cron.daily directory or create a symbolic link to it using cd /etc/cron* -d. If you want to run it using a different schedule, just drop it into the cron.hourly, cron.monthly, or cron.weekly directories. Just remove the script or the symbolic link from the folder if you don't want to execute it anymore. If you don't want to run system-wide cron jobs, you can also use the crontab command as a normal user. You can read the crontab manual to learn more about this command. Finally, let's test whether the motd file is working. Exit out of the SSH session and then log in again:
As you can see, it's working beautifully. Based on the cron job we created, tomorrow there should be a different command-line example presented here.
In this chapter, we have introduced you to the Linux cron system for script automation.