Paul Sutton

crontab

Bash scripting 21 – cron

One of the important things you need to do as a system admin is run tasks that perform specific tasks on the system. This is fine when you are sitting in front of the computer, or logged in remotely, but what happens if you need to run a task when you are not there, or you just want to automate the running of these tasks.

You can schedule tasks using the cron command. There is an easy-to-use tutorial for this at [1]. I am not going to reproduce this here, but may include some of my own specific use cases in a later post.

According to [1] the following

*/5  * * * * /home/paul/crontest.sh

runs every 5 minutes, where as

*/10  * * * * /home/paul/crontest.sh

So putting ls -l in crontest.sh will run that command every 5 or 10 minutes.

However, I am logged into my pi (where I am running this) so the script will run, but I don't see any output.

If I add in ls -l > dirlist.txt

It will produce a directory listing text file when run, by running ls manually you can see it in the listing.

I have a post on Ansiweather scheduled fora few days, in the meantime

putting this in the crontest.sh file

echo $(date), $(ansiweather -l Paignton -a false) >> weather.csv

It will run at the specified time and append the information to weather.csv which could be a good way to monitor weather over time.

This can be examined with

cat weather.csv 

if you use the watch command

watch cat weather.csv

the contents of weather.csv will be displayed and updated in real time as the file is appended with new information.

References

  1. https://stackoverflow.com/questions/12786410/run-cron-job-every-n-minutes-plus-offset

Tags

#Bash,#Bashscripting,#Task,#Schedule,#cron,#crontab,#Admin, #Automation


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay