Why doesn't my cronjob execute as expected? - python

I edit the crontab in Ubuntu 18.04:
crontab -e
50 21 * * * /data/min/query/sync.sh >> logs/tag.log
Then I saved it and exited. I did this at 21:41pm, and I suppose it will execute it at 21:50pm, but it didn't effect. I checked the logs/tag.log, but nothing occurs.
The content of my sync.sh:
#!/bin/bash
cd /data/min/query
echo 'sync tags now ...'
source venv/bin/activate
python utils/tag_counter.py
echo 'updating resource files are done!!!'
I make the script executable by:
chmod +x sync.sh
So that sync.sh can be executed directly. The script does work when executed at the command line, but not working in the cronjobs. This is the first time I use cronjob. Is there anything I am missing in setting up the cronjob?

Related

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.

crontab won't run os.system python command

Using ubuntu's 16.04 crontab and #reboot to run python3 script. The script runs properly on reboot as I see the logged output. However, my script's os.system command is not running. It runs fine if ran outside of crontab. My scripts are all executable.
crontab -l output:
SHELL=/bin/bash
#reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py code:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
When I logged the output of the os.system command , there was no output.
As a side note, I am running python schedule commands to check the general health of a webserver. crontab doesn't seem to be the right tool for this so I just use crontab to start my python scheduler on reboot.
I am using flask as the webserver, and would use gunicorn and systemctrl if I could get it to work... but it didn't so this is my workaround.
The point is that, the command called by os.system is not in default path.
For example, tcpdump is not in /usr/bin/.
So, you can solve the problem by adding the full path of the command.
I was facing the same issue when we try to run python script directly in crontab it just by passes the os.system() commands.
Make launcher.sh:
#!bin/bash
cd /home/pi/
sudo python example.py
Then, make your script executable:
chmod 755 launcher.sh
And at last, add your script to crontab:
crontab -e
and add this line at the end:
#reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)

Using cron to launch python script in terminal window after reboot

I am trying to launch a python script automatically after boot. I want to launch it in a terminal window because the program gives important feedback in the terminal.
I've researched many ways to do this including crontab, init.d, rc.local, and /etc/xdg/autostart/myscript.desktop.
When I've tested my script manually, calling rc.local from the terminal works, however none of these work after booting.
I've tried many variations, the latest in crontab:
#reboot sleep 60 && xterm -hold -e sudo python /home/pi/newcode/newcode/boot-test.py
Other variations I tried include (calling my python script from a shell script):
#reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh
and
#reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh
Update:
foo1.desktop (saved at /usr/local/bin/foo1):
[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;
foo1 (saved at /etc/xdg/autostart/foo1.desktop):
#!/bin/bash
/usr/bin/x-terminal-emulator --command="/home/pi/newcode/newcode/boot-test.py" --title="My foo1"
python script, very simple for now (saved at /home/pi/newcode/newcode/boot-test.py)
sensortype=raw_input("press enter to continue")
Comment:
1. is named foo1.desktop ?
2. is named foo1.sh ?
3. does foo1.sh need to be made executable?
Yes, must be named *.desktop
If you use *.sh, the entry in *.desktop have to be equal
Exec=gksu /usr/local/bin/foo1.sh
Yes, a Bash script have to made executable.
Question: I've test my script (rc.local) manually
Don't use etc/rc.local, it's executed to early for X11.
My /etc/xdg/autostart example:
Create a foo1.desktop with at least the following content:
[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;
Copy foo1.desktop or create link to foo1.desktop in /etc/xdg/autostart.
Create /usr/local/bin/foo1 with the following content:
#!/bin/bash
/usr/bin/x-terminal-emulator --command="python path_to_your_*.py" --title="My foo1"
Make /usr/local/bin/foo1, executable.
After trying multiple variation the script that worked contained the following:
#!/bin/bash
/usr/bin/x-terminal-emulator -hold -e sudo python /home/pi/newcode/newcode/hms2-v2.6.py
making the script executable by do the following was also necessary:
sudo chmod 755 /etc/xdg/autostart/foo1.desktop
I never got cron or init.d to work

Can't execute a cron job

I am creating a cron job to execute a python script
hello.py
a = 'a cron job was executed here'
text_file = open('output_hello.txt', 'w')
text_file.write(a)
text_file.close()
Works fine if I execute via terminal, I am on ubuntu 15.10.
My cron job file is:
* * * * * /usr/bin/python /home/rohit/hello.py
(excluding the #)
I am a root user and creating the job in /var/spool/cron
The issue is that it is not executing the script. I don't know why.
One does not simply modify the crontab, you run the command:
crontab -e
and edit from there. Execute the above command using sudo if you want it to run as root.
Assuming your paths are correct, your script may not have the right environment or it may not be executable. Ensure your script starts with:
#!/usr/bin/python
And also that you then give execute permission to that script:
chmod a+x hello.py
Ensure you use crontab -e and if you have any doubts about your syntax, you can find more info here:
https://help.ubuntu.com/community/CronHowto

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