Code Done Right!

Running script

Ever wondered if there is any useful script around there to help you with your daily work on Linux? There is. Quite plenty. I will show you how to run script manually and how to automate executing script so that it runs whenever you want it to run. Read on!

Getting script

Before you run any code, you have to have some, logical, right? The best source of it is you. Only you know what do you actually need. What is the point of running a code that, even though useful in general, it is not useful to YOU?

Rule of thumb is that if you have a task that you are performing with ANY regularity then you can write a script that will do that for you. The only question is how to do it. I encourage you to grab a book about working with CLI, like this one for example – The Linux Command Line, 2nd Edition: A Complete Introduction. But for now I expect you will be grabbing script from GitHub or other Raspberry related sites and wondering how to make it work.

Creating script file

In order to create script just create a new file with .sh extension, e.g. example_script.sh

nano example_script.sh

Each script file needs to start with a shebang#! after which you specify the shell that will execute the script. Default Raspbian/Debian shell is Bash.

#!/bin/bash

In order to execute the script the file needs execute permissions. Run the following

sudo chmod +x example_script.sh

Example script file

#!/bin/bash

echo "Hello world!"

Running script – manually

In order to execute a script file named example_script.sh you just have to type the name prefixing it with ./ like so

./example_script.sh

But you have to be in the same directory where the script is located.

If you are in a different directory, instead of prefixing the name with ./ just use the full path to your script, like so

/path/to/script/example_script.sh

That is basically all, after you press ENTER, the script is executed.

Running script – automatically

Utilizing a tool called cron you can automate execution of script. Cron comes with Linux by default, so you do not have to install any additional packages.

CAUTION as the script will be executed without user input, do not move the script after adding it to a crontab!

If you followed the basic security tutorial a directory for your custom script is already there, otherwise create a directory where you will keep any automated script. It is more convinient to keep it all in one place.

Now place the script that is supposed to be executed inside that folder (check Basic commands), make it executable and run the following command

crontab -e

This will bring up a file in which, at the very bottom, you will put instruction on when and what to execute. Scheduling tasks has the following form

* * * * * /path/to/script

Asterisks are responsible for (from left to right)

  • Minute
  • Hour
  • Day of month
  • Month
  • Day of week

You can substitute asterisk with a number to define when the task should be performed.

Leaving an asterisk * will mean at every. Keep that in mind and take a look at a few examples

* * * * * /path/to/script

Above line means every minute. Script will run every 60 seconds!

Unless you have a very specific usage for running script every minute, this may utilize so much of you system resources that your server will grind to a halt. It will add a new task on top on an unfinished one to infinity and beyond. Or until everything breaks.

0 * * * * /path/to/script

Above line will make the script run every hour. It translates to At minute zero.

0 0 1 10 * /path/to/script

This one is for when you want to wake up Billie Joe Armstrong. It translates to Minute zero of hour zero of the first of October.

If you have any trouble setting up a timeframe for your crontab use this online tool to help you.

Few things to keep in mind when using crontab

  • Do not schedule many tasks for the same time, it will unnecessarily consume resources and slow down your server
  • Schedule tasks one after another, make sure you roughly know how much a certain task will take – e.g. full data backup may take hours
  • Keep the file organized! Put tasks in order of the most frequent at the top of your list to the least frequent at the bottom, it is for your sake

Test the execution of crontabbed script by setting it up 2 minutes in the future then close the file. If all goes well you will have the result shortly and afterwards you can change the frequency.

sudo crontab?

Is there a difference between running crontab as current user and root? Yes, gigantic!

crontab -e opens crontab for the CURRENT USER, and when you run the command with sudo the current user is… root. Remember that each user has its own crontab.

Unless you need to perform a task that needs administrative privileges, execute everything as your current user. Overusing sudo is a potential security breach. You should read up on the topic, but for now just trust me. Do not run commands as root unless you absolutely need to.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.