How to Run the Cron Job as a User Instead of Root User

How to run the cron job as a user instead of root user

There are several possibilities:

1) add the script(s) to the crontab of root. To do this you have to do sudo su - or su - to become root, then add the cron jobs by using crontab -e

2) allow a non-root user to use a crontab, and add the cron job to that user's crontab , by using crontab -e
and set the set-uid flag of your script and change ownership to root, so it will execute as root chmod +s scriptname; chown root scriptname

How to run a cron job as a non-root user and log the job's output?

The Alpine base images are based on a compact tool set called BusyBox and when you run crond here you're getting the BusyBox cron and not any other implementation. Its documentation is a little sparse, but if you look at the crond source (in C) what you'll find is that there is not any redirection at all when it goes to run a job (see the non-sendmail version of start_one_job); the job's stdout and stderr are crond's stdout and stderr. In Docker, since crond is the container primary process, that in turn becomes the container's output stream.

Anything that shows up in docker logs definitionally went to stdout or stderr or the container's main process. If this cron implementation wrote your job's output directly there, there's nothing wrong or insecure with taking advantage of that.

In heavier-weight container orchestration systems, there is some way to run a container on a schedule (Kubernetes CronJobs, Nomad periodic jobs). You might find it easier and more consistent with these systems to set up a container that runs your job once and then exits, and then to set up the host's cron to run your container (necessarily, as root).

How to specify in crontab by what user to run script?

Instead of creating a crontab to run as the root user, create a crontab for the user that you want to run the script. In your case, crontab -u www-data -e will edit the crontab for the www-data user. Just put your full command in there and remove it from the root user's crontab.

Cron, execute bash script as root, but one part (Python script) as user

Well, if the dummy folders did get created, that means the sudo statements work, so i'd say theres a 99%+ chance that python was infact started.

I'm guessing the problem is that you havent specified the path for the python file, and your working directory likely isn't what you're expecting it to be.

change:

sudo -u user_name /usr/bin/python3 python_script.py --arg01 --arg02

to something like

sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02

then test.

If that didn't solve the problem , then enable some logging, change the line to:

sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02 \
1> /home/user_name/projects/dummy_folder_00/log.txt 2>&1 ;

and test again, it should log STDOUT and STDERR to that file then.

Cron job not running automatically for a non-root user

The correct approach to run your cron every midnight is:

00 00 * * * /bin/bash path/to/your/script.sh >> /path/to/log/file.log


Related Topics



Leave a reply



Submit