crontab won't run os.system python command - python

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)

Related

Why doesn't my cronjob execute as expected?

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?

bash script in rc.local different result than executing it in terminal

I'm trying to run a python script from a headless raspberrypi that uses the pynput library.
In order to use pynput on a headleass machine it requires you to do the following steps on every bootup:
export DISPLAY=:0
DISPLAY=:0 python -c 'import pynput'
If I don't do this I get the following error:
ImportError: this platform is not supported: ('failed to acquire X
connection: >
Try one of the following resolutions:
Please make sure that you have an X server running, and that the
DISPLAY env>
If I now try to automaticate this process through a bash script that contains the following lines:
#!/bin/bash
sleep 60
export DISPLAY=:0
sudo chmod 666 /dev/hidg0
DISPLAY=:0 python -c 'import pynput'
python3 /home/pi/Hid.py |& tee -a /home/pi/test.log
exit 0
And execute the file normally like this from terminal:
bash /home/pi/bash.sh
it works perfectly fine.
But when I mention the script in the rc.local file the same way I executed it before, the script is getting executed on boot, but the python script throws the error that was shown at the beginning.
How can there be two different outcomes, when it's getting executed by hand and when it's getting executed by a bash script at boot via. rc.local?
Any help is appreciated!
All commands from rc.local are getting executed by user root.
To execute them from a different user use:
sudo su USERNAME -c 'SCRIPT/PATH'

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.

Run Python script at startup in Ubuntu

I have a short Python script that needs to run at startup - Ubuntu 13.10. I have tried everything I can think of but can't get it to run. The script:
#!/usr/bin/python
import time
with open("/home/username/Desktop/startup.txt", 'a') as f:
f.write(str(time.time()) + " It worked!")
(The actual script is a bit different, as I'm just using this for testing purposes, but you get the idea.)
I've tried all of the following, with no luck:
Put the command python startuptest.py in crontab, as #reboot
python /home/username/Documents/startuptest.py, both as the regular user and as sudo
Put the command python /home/username/Documents/startuptest.py in /etc/rc.local
Opened Ubuntu's Startup Applications and put the command there
Done all of the preceding, putting the command into a shell script
and calling that shell script instead
Nothing works. I get the feeling I'm missing something simple. Any ideas? (The script runs fine if I just run the command from a terminal.)
Instructions
Copy the python file to /bin:
sudo cp -i /path/to/your_script.py /bin
Add A New Cron Job:
sudo crontab -e
Scroll to the bottom and add the following line (after all the #'s):
#reboot python /bin/your_script.py &
The “&” at the end of the line means the command is run in the background and it won’t stop the system booting up.
Test it:
sudo reboot
Practical example:
Add this file to your Desktop: test_code.py (run it to check that it works for you)
from os.path import expanduser
import datetime
file = open(expanduser("~") + '/Desktop/HERE.txt', 'w')
file.write("It worked!\n" + str(datetime.datetime.now()))
file.close()
Run the following commands:
sudo cp -i ~/Desktop/test_code.py /bin
sudo crontab -e
Add the following line and save it:
#reboot python /bin/test_code.py &
Now reboot your computer and you should find a new file on your Desktop: HERE.txt
Put this in /etc/init (Use /etc/systemd in Ubuntu 15.x)
mystartupscript.conf
start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/script.py
By placing this conf file there you hook into ubuntu's upstart service that runs services on startup.
manual starting/stopping is done with
sudo service mystartupscript start
and
sudo service mystartupscript stop
If you are on Ubuntu you don't need to write any other code except your Python file's code , Here are the Steps :-
Open Dash (The First Icon In Sidebar).
Then type Startup Applications and open that app.
Here Click the Add Button on the right.
There fill in the details and in the command area browse for your Python File and click Ok.
Test it by Restarting System . Done . Enjoy !!
Create file ~/.config/autostart/MyScript.desktop
with
[Desktop Entry]
Encoding=UTF-8
Name=MyScript
Comment=MyScript
Icon=gnome-info
Exec=python /home/your_path/script.py
Terminal=false
Type=Application
Categories=
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=0
It helps me!
In similar situations, I've done well by putting something like the following into /etc/rc.local:
cd /path/to/my/script
./my_script.py &
cd -
echo `date +%Y-%b-%d_%H:%M:%S` > /tmp/ran_rc_local # check that rc.local ran
This has worked on multiple versions of Fedora and on Ubuntu 14.04 LTS, for both python and perl scripts.
nano /etc/rc.local
and edit in
python ~/path-to-script.py
worked for me

Categories

Resources