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
How to Check If Ssh-Agent Is Already Running in Bash
Using Curl in a Bash Script and Getting Curl: (3) Illegal Characters Found in Url
How to Forward Localhost Port on My Container to Localhost on My Host
How to Signal Handler to Survive After "Exec"
What Is the Linux Built-In Driver Load Order
Position of a String Within a String Using Linux Shell Script
Sed: -I May Not Be Used with Stdin on MAC Os X
Maintain File and Folder Permissions Inside Archives
How to Connect to the Fitbit Zip Over Bluetooth 4.0 Le on Linux with Bluez
Error While Using a Newer Version of Glibc
How to Check Fips 140-2 Support in Openssl
Bash: Read Stdin from File and Write Stdout to File
How to Read the Mouse Button State from /Dev/Input/Mice
How to Specify Non-Capturing Groups in Sed
How to Tell If a Linux MAChine Supports Avx/Avx2 Instructions