Running a script with Crontab by a user

For certain needs, it may be necessary to execute a script via crontab by a user other than root. In this tutorial, I will use www-data because my script will be related to my Apache server.

Once your script is created, place it in the appropriate folder. For me, it will be /usr/local/bin/

Let’s assign the proper permissions!

chown www-data:www-data /usr/local/bin/mysuperscript.sh
chmod 550 /usr/local/bin/mysuperscript.sh

A quick reminder:

chown The www-data user and the group of the same name will be the new owners of the script.

5 The www-data user will have read and execute rights

5 Users belonging to the group will have read and execute rights

0 Others will have no rights on this script

Now, we will schedule our task.

Open Crontab with the -u user option

crontab -u www-data -e

To disable the emails that crontab sends after every execution, add this line:

MAILTO=""

Then add your command.

Here, I run my script (monsuperscript.sh) every minute (* * * * *)

* * * * * /usr/local/bin/monsuperscript.sh

It may be necessary to specify the /bin/bash shell in the Crontab in case of an error.

* * * * * /bin/bash /usr/local/bin/monsuperscript.sh

Leave a Comment