We ❤️ Open Source
A community education resource
Automating jobs with ‘cron’
Things can go more smoothly if you automate them to run according to a schedule.
I run a few Linux systems at home, including a Raspberry Pi as a home “server.” It does several useful things for me, including a print server for an old laser printer that doesn’t support Wi-Fi networks. As a server, it’s important that I can communicate with the Pi whenever I need it. And for several years, it’s done a great job of that.
A few months ago, my cable company sent me a new router with a built-in Wi-Fi access point, as part of their regular upgrades for all customers. This was fine, until a few days later when I realized my Raspberry Pi had been disconnected from the network. After a little debugging, I realized that the Pi was still running and trying to talk to the network, but the new router had taken it off the air after about two days.
Ideally, I would have adjusted the configuration on the router to keep the Raspberry Pi connected to my home network. But while the new router lets me assign a static IP address to the Pi, it doesn’t have a setting to keep the Pi connected to Wi-Fi.
Automating a job
My workaround was to create a job that would disconnect the Raspberry Pi from the Wi-Fi network, then reconnect to the network. By disconnecting then reconnecting, the Pi essentially becomes a “new” client on the network, and the router recognizes it for another two days.To disconnect the Raspberry Pi from the Wi-Fi network, I needed to run a Network Manager command line interface (nmcli) command to bring “down” the Wi-Fi interface:
nmcli con down id mywifiname
Another nmcli command will reconnect the Pi again:
nmcli con up id mywifiname
I decided to run these commands once a day, which was well under the “two days” limit my router imposes on network connections. Since I only need the Raspberry Pi when I’m awake, I could disconnect the Pi from the network in the wee hours of the night.
Using ‘cron’ to automate tasks
Linux provides several ways to automate tasks, depending on how you want to run them. For example, at will run a task once at a specific time. The cron system will run various jobs at regular intervals, on a schedule you specify.
The cron system is controlled through a plain text file. This is arranged like a “table” called a crontab file, where each line represents a different task to automate. Tasks can be a single command or a shell script with multiple instructions. Every line in the crontab has five fields plus the command to run. The first five fields indicate the schedule:
minute | 0 to 59, or * for every minute |
hour | 0 to 23 (24-hour time), or * for every hour |
day of the month | 1 to 31, or * for every day of the month |
month | 1 to 12, or * for every month |
day of the week | 0 to 7 (0 and 7 are Sunday), or * for every day |
For example, to run the foo command every hour on the hour, you would create a line in the crontab file like this:
0 * * * * foo
In my case, I wanted to disconnect the Raspberry Pi from the network at 1:00am, and reconnect it an hour later at 2:00am. There’s no special reason to run these an hour apart; this was just the easiest way to enter the commands into my crontab file at a time when I was unlikely to use the Pi. I created a new file called myjobs.txt as the root user on my Pi:
0 1 * * * nmcli con down id mywifiname
0 2 * * * nmcli con up id mywifiname
The first line runs the nmcli con down id mywifiname command whenever the “minutes” part of the time is zero, the “hour” is 1, for any day of the month, in any month, on any day of the week. Similarly, the second line runs the nmcli con up id mywifiname instruction whenever the “minutes” is zero, the “hour” is 2, for any day of the month, any month, any day of the week.
After you have put all of your automated tasks in the myjobs.txt file, you can tell the cron system to use it by loading the file with the crontab command:
# crontab myjobs.txt
In my case, I can only run the nmcli commands as root, so I needed to run the crontab command as root. But any user can use cron to automate tasks.
To verify that the right automation schedule has been loaded, you can display the current crontab file with the -l (list) option:
# crontab -l
0 1 * * * nmcli con down id mywifiname
0 2 * * * nmcli con up id mywifiname
If you don’t need the automated jobs anymore, you can remove the crontab file with the -r (remove) option:
# crontab -r
Automation makes things go more smoothly
While job automation may seem like something that only a server administrator might do, it’s something that can help everyday users too. You can explore the other options for the crontab command by looking at the online manual with man crontab.It’s also worth exploring other ways to schedule jobs, such as specifying values like 1, 3, 5 to only run jobs at 1:00am, 3:00am, and 5:00am. You can also specify ranges like 1-4 to run jobs only on the first four days of a month. Learn more about the different ways to enter a schedule in “section 5” of the online manual, with man 5 crontab.
The opinions expressed on this website are those of each author, not of the author's employer or All Things Open/We Love Open Source.