Python script inside docker creates empty file with CRON - python

I have a CRON job set like this :
0 0 * * * cd /home/path/to/script && sudo -u myuser ./thescript.sh
This script builds a docker image running a simple python app dumping on file a test validation report. When I run this script in a terminal everything works fine (the generated file goes in /home/myuser). Unfortunately, when I run the CRON job, the file is created, but empty. It must have something to do with the Root owning the CRON job, but I can't figure out how to get it done
Any clue ?

A sudo requires a TTY and cron doesn't run commands with a TTY. You need to run the cron for root user
This can be done using
sudo crontab -e
Then in cron don't use sudo
0 0 * * * cd /home/path/to/script && ./thescript.sh

Related

problem with cronjob to run python script on ubuntu server

I have installed python3 following the Digital Ocean guide and to run my python scripts just by command line, this is the bash I use and it works:
~$ source /home/username/python_projects/project_name/bin/activate
~$ python3 /home/username/python_projects/project_name.py
~$ deactivate
if I put that commands in crontab in the same order, nothing happens.
0 7 * * * source /home/username/python_projects/project_name/bin/activate
0 7 * * * python3 /home/username/python_projects/project_name.py
0 7 * * * deactivate
What am I doing wrong?
Cronjob is active and running.
~$ systemctl status cron
The python file has this permissions:
-rw-rw-r-- 1 user_name user_name 17075 Feb 7 02:30 python_projects/project_name.py
The activate job runs, then exits, and then the next cron job knows nothing about what it did. You want to run them all in the same process (though running deactivate at the end is pointless, as everything that activate did will be wiped when the job ends anyway).
In practice, you can run the Python interpreter from within the virtual environment directly.
For what it's worth, the error message about "no MTA installed" means that the output from cron was discarded because the system could not figure out how to send it by email. You'll probably want to change the rule to write the output to a file.
0 7 * * * cd python_projects && ./project_name/bin/python3 project_name.py >>project_name.log 2>&1
Perhaps notice that cron always starts in your home directory, so you don't have to spell out /home/username (provided of course that username is the user whose crontab this runs from).
There are a few things that could be causing your cronjob not to run:
Environmental variables: The source command in your cronjob sets up the virtual environment, but this environment is not passed on to the cron process. You should specify the full path to the Python executable in your virtual environment, e.g. /home/username/python_projects/project_name/bin/python3.
Permission issues: Ensure that the cron user has permission to execute the script and access the virtual environment.
Output: By default, cron does not send any output to the terminal. If your script produces output, you may want to redirect it to a file for debugging purposes. You can do this by adding > /tmp/output.log 2>&1 to the end of your crontab entry.
You can check the system logs for any error messages related to your cron job. The logs are usually located in /var/log/syslog or /var/log/messages.
I hope this helps!

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

I need to run a bash script periodically on a Jetson Nano (so, Ubuntu 18.04). The script should run system updates, pull some Python code from a repository, and run it as a specified user.
So, I created this script:
#! /bin/bash
## system updates
sudo apt update
sudo apt upgrade
## stop previous instances of the Python code
pkill python3
## move to python script folder
cd /home/user_name/projects/my_folder
## pull updates from repo
git stash
git pull
## create dummy folder to check bash script execution to this point
sudo -u user_name mkdir /home/user_name/projects/dummy_folder_00
## launch python script
sudo -u user_name /usr/bin/python3 python_script.py --arg01 --arg02
## create dummy folder to check bash script execution to this point
sudo -u user_name mkdir /home/user_name/projects/dummy_folder_01
I created a cron job running this script as root, by using
sudo crontab -e
and adding the entry
00 13 * * * /home/user_name/projects/my_folder/script.sh
Now, I can see that at the configured time, both the dummy folders are created, and they actually belong to user_name. However, the Python script isn't launched.
I tried creating the cron job as non root user (crontab -e), but at this point even if the Python script gets exectured, I guess I wouldn't be able to run apt update/upgrade.
How can I fix this?
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 python file doesn't work on centos 7

i'm trying to schedule my pythohn script into Centos 7 with cron.
On my script at start i have added this:
#!/usr/local/bin/python
and this is my cron file that i have create into folder that contain python file
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
i have try to run the script into terminal by chmod +x and works fine.
But whit this configuration when in terminal i set crontab .cron the job doesn't work. The log file set into cron file are not write and script not run. The script could be write some data into db and the db is always empty.
Any help???
Thanks
Type crontab -e in the shell, this will open the cron editor. Here copy paste the entire command.
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
Now press Esc key then type colon :wq! and press Enter key. This will install your cron. Your cron should run every 5 minutes.
Your standard output and error will be appended to /home/Desktop/log.txt.

Docker, dockercloud and Cron

I have a dockerfile which automates the building of an image.
I am using the docker cloud, connected to Digital Ocean as the server.
Within my dockerfile, I get the software I need, add the relevant GitHub repository containing the python scripts I wish to run. I then start the cron scheduler and add the script with appropriate times. For example:
The cron_files.txt file looks like this:
0 12 * * * /usr/bin/python /home/dir/run_check.py
0 15 * * * /usr/bin/python /home/dir/run_push.py
In my dockerfile, I do the following:
RUN service cron start
RUN service cron status
RUN crontab -u root cron_files.txt
In the log files, I can see that cron is succesfully started.
Edit, thanks to r0manarmy for this - How to run a cron job inside a docker container?
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
How do I edit the above to create the crontab file from the cron_files.txt rather than the above example?
I've tried ADD crontab cron_files.txt /etc/cron.d/feeds
But this returns:
ADD crontab cron_files.txt /etc/cron.d/feeds
lstat crontab: no such file or directory
Ps. I am using FROM debian:jessie
You probably want to set cron as the CMD:
In order to do this, just use the crond command on, say, alpine linux.
Take a look at this example for ideas:
https://gist.github.com/mhubig/a01276e17496e9fd6648cf426d9ceeec

Run Python Script Using Cygwin Crontab on Windows

I succeed in setting up Cygwin Crontab on Windows. I need to run Python script every 5 minutes.
*/5 * * * * run-one C:\Python27\Scripts\myScript.py > C:\Python27\Scripts\myScript.log 2>&1
Above didn't work in crotab. Also the log file cygstart cron.log is empty. How I'm suppose to fix and run the script?
P.S. Script is running fine using idle editor.
Run your script directly in the Cygwin command line:
C:\Python27\Scripts\myScript.py > C:\Python27\Scripts\myScript.log 2>&1
If it does not work, you may need to specify python.exe directly and use forward slash ("/") rather than backslash ("\"):
C:/Python27/python.exe C:/Python27/Scripts/myScript.py > C:/Python27/Scripts/myScript.log 2>&1
If it works, then please make sure cron is running. If it is running, it should give similar output as below:
$ cygcheck.exe -c | grep cron
cron 4.1-61 OK
cron-debuginfo 4.1-61 OK
$ cygrunsrv -Q cron
Service : cron
Display name : Cron daemon
Current State : Running
Controls Accepted : Stop
Command : /usr/sbin/cron -n
$ ps -lef | grep cron
SYSTEM 4852 4680 ? 15:16:50 /usr/sbin/cron
If it is not running, run
$ cygrunsrv --start cron
If there is an error, you may need to reinstall cron.
$ cyglsa-config
Then
$ cron-config
And follow the instructions.

Categories

Resources