Skip to content
Advertisement

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.

This is my script (scritp.sh) which permission are 700:

#!/bin/sh
clear
echo "Hello!"
mkdir Hello

And this is the crontab file when I edit it with the command crontab -e:

SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh

EDIT: I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can’t see the echo of the script on the terminal.

EDIT2: I have added mkdir hello, because I don’t know if the echo of the script is shown on the terminal. But the hello directory is never created.

Advertisement

Answer

Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I’d suggest that you look in your inbox on the local machine.

To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.

Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.

The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script’s working directory is, use cd in the script to move to the correct location.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement