How to Run a Shell Script by Cron Job

Running a simple shell script as a cronjob

What directory is file.txt in? cron runs jobs in your home directory, so unless your script cds somewhere else, that's where it's going to look for/create file.txt.

EDIT: When you refer to a file without specifying its full path (e.g. file.txt, as opposed to the full path /home/myUser/scripts/file.txt) in shell, it's taken that you're referring to a file in your current working directory. When you run a script (whether interactively or via crontab), the script's working directory has nothing at all to do with the location of the script itself; instead, it's inherited from whatever ran the script.

Thus, if you cd (change working directory) to the directory the script's in and then run it, file.txt will refer to a file in the same directory as the script. But if you don't cd there first, file.txt will refer to a file in whatever directory you happen to be in when you ran the script. For instance, if your home directory is /home/myUser, and you open a new shell and immediately run the script (as scripts/test.sh or /home/myUser/scripts/test.sh; ./test.sh won't work), it'll touch the file /home/myUser/file.txt because /home/myUser is your current working directory (and therefore the script's).

When you run a script from cron, it does essentially the same thing: it runs it with the working directory set to your home directory. Thus all file references in the script are taken relative to your home directory, unless the script cds somewhere else or specifies an absolute path to the file.

How to run the shell script in cron job in specific time at only once per day

The crontab you've listed means;

* 9 * * *
> At every minute past hour 9

You'l need 0 9 * * * since you only want to job to run on the whole our, hence the 0 minutes.

0 9 * * *
> At 09:00

Use something like crontab.guru to visualize it.

How to run linux commands using cron

You can create a shell script with this commands in a file script.sh, for example:

#!/usr/bin/bash

rm -rf <directory>
mkdir <directory>
chmod 777 <directory>

<others commands or logical instructions>...

In linux you can add a cron job in crontab, with crontab -e command, or in /etc/cron.d directory. The difference is that with command crontab -e the cron job will be set to user that execute crontab -e and add a cron job file right in cron.d you will need to put the user ahead the command of cron job.

Examples of cron jobs to be executed at 06:00 am.

With crontab -e:

0 6 * * * /usr/bin/bash /path_to_script/script.sh

Creating a file in /etc/cron.d:

0 6 * * * root /usr/bin/bash /path_to_script/script.sh

Alternatively you can just put the commands in your cron job as:

0 6 * * * rm -rf <directory> && mkdir <directory> && chmod 777 <directory>

Attention: remember to put the absolute path to directories that want to remove or create

PS: you can make your scripts in any language and use shell calls, as php with shell_exec() function or system() function of perl.

How to combine these 2 scripts and run multiple cron jobs for the same script to get different outputs?

Make your script run every 30 minutes. On success, check if it is 11am before outputting success.

if [ $Status == "200" ]; then
if [ $(date +%H%M) == "1100" ]; then
echo "success"
fi
else
echo "error"
fi

Note: This checks for exactly 11:00 am. If your script may take longer than 1 minute to reach this point, save the time somewhere at the beginning time="$(date +%H%M)" and check against it afterwards if [ "$time" == "1100" ].


Alternatively, if you want to keep the actual time at which to behave differently outside the script, you can introduce a parameter that is set only (or in one way) on the 11 am run, but not (or in another way) on the other runs.

This requires you to still have multiple cron jobs, but with different parameters to the same script.

   0 11         * * * /home/username/script.sh yes
30 11 * * * /home/username/script.sh no
*/30 0-10,12-23 * * * /home/username/script.sh no

Within the script you need to check the parameter provided.

printsuccess="$1"

if [ $Status == "200" ]; then
if [ $printsuccess == "yes" ]; then
echo "success"
fi
else
echo "error"
fi


Related Topics



Leave a reply



Submit