Restart Python Code after restarting the server - python

I have a Python code running on Spyder on our server which needs to run constantly. However, from time to time our server breaks and it is restarted. Unfortunately, my code stops running as well and I need to restart Spyder and the code manually when opening it the next day.
Is there any way to restart the code automatically when the server is restarted?
Thank you a lot!

You can add as a crontab rule what you want to run after reboot.
You can use this line in crontab by using crontab -e command, if you use server running on Linux platform.
#reboot <your_python_file_path>

Simply utilize shell:startup and add your Python location + path there (or create a Batch script for it):
myprogram.bat:
python.exe main.py
Alternatively, if you don't have python location in your PATH env variable, utilize the full path:
myprogram.bat:
"C:\Program Files\Python39\python.exe" main.py

Related

Ubuntu EC2 - Run Python script on reboot

I have an Ubuntu 16.04 EC2 Instance and I need to run a python script every time the instance is Started.
I tried everything suggested on every question in the forum and haven't had any luck yet.
Specifically I've tested:
Adding #reboot python3 /home/project/script.py to crontab
Adding #reboot /bin/startup.sh and having the bash file configured to run the /home/project/script.py
Using etc/rc.local, etc/init/mystartup.conf, etc/systemd/mystartup.conf
Passing User Data
Probably missing a few others and literally nothing worked even though running the script manually works wonders.
Thanks a lot in advance for the help!
Put the script into: /var/lib/cloud/scripts/per-boot/
Cloud-Init, which runs User Data, will also check this directory.
From Modules — cloud-init documentation:
Any scripts in the scripts/per-boot directory on the datasource will be run every time the system boots. Scripts will be run in alphabetical order.

Automatically run graphical python script on startup in ubuntu

I have a python script that starts a fullscreen animation when run - how do i set this to start at startup in ubuntu 16.04? Other answers to the "script on startup" problem don't work - because I'm assuming they're executed before the desktop environment has a chance to start up...
I have a script called "startup.sh" that reads as:
xrandr --output HDMI-0 -s 1280x1024
source activate demo
cd ~/demo/
python run.py
Where the first line sets the correct resolution - the second line activates an anaconda environment, the third line changes directory (parts of the script need it to be in that directory, and the 4th line finally runs the script that launches a fullscreen graphic (using pygame, if it matters.)
How can i set this to auto run after unity finishes setting up? Thanks so much... any help or comments at all are really appreciated...
EDIT: it looks like someone here had a similar problem: https://www.raspberrypi.org/forums/viewtopic.php?t=128602
They wanted to start a pygame script on startup - and were able to do so after their desktop environment loaded by putting a call to their program in ~/.config/lxsession/LXDE-pi/autostart - I'm not using LXDE - i'm on unity, so is there an equivalent?
Easiest way to run things at startup is to add them to the file /etc/rc.local.
But to do this properly, you should use the update-rc.d command. You may try it like this:
update-rc.d /path_to_script/script_name.sh start 2
Or use update-rc.d /path_to_script/script_name.sh defaults to create both startup and kill scripts all at once.
For more precise actions you can visit here for details information: http://manpages.ubuntu.com/manpages/xenial/man8/update-rc.d.8.html.

Cordova command not running through cron job

I am trying to make one cron job script in Python . For the start what i did i just add the code to run the cordova and show its version the same python file works if i run it through shell but when its run through cron job it gives me this error
env: node: No such file or directory
Python file has this code
#!/usr/bin/python
import os
import subprocess
subprocess.call('/usr/local/bin/cordova -v',shell=True)
Is there any solution for that ? What i get to understand that maybe when i run code through cronjob the global variables are not accessable . Is there anyway that i can get access to command line terminal through pipe and get access to all the global variables ?
Please use env command from your terminal and cronjob script (e.g.: env > cron_output file), it will give the environment variables available for normal terminal and cronjob. Compare that variables and export the required missing variables in your script.

Cron job can't run bash script (that shells python script requiring xsession) that runs perfectly well from console

I am using dryscrape in a python script. The python script is called in a bash script, which is run by cron. For those who may not be aware, dryscrape is a headless browser (use QtWebkit in the background - so requires an xsession).
Here are the main points concerning the issue I'm having
When I run the python script from the command line, it works
When I run the bash script from the command line, it works too
I figured out that this may have something to do with different environments between my command prompt and when the cron job is running, so I modified my bash script to source my .profile as follows:
#/bin/bash
. /full/path/to/my/home/directory/.profile
python script_to_run.py
This is what my cronjob crontab entry looks like:
0,55 14-22 * * 1-5 /path/to/script.sh >> $(date "+/path/to/logs/\%Y\%m\%d.mydownload.log" )
By the way, I know that the job is being run (I can see entries in /var/log/syslog, and the script also writes to a log file - which is where I get the error message below):
In all cases, I got the following error message:
Could not connect to X server. Try calling dryscrape.start_xvfb()
before creating a session
I have installed the prerequisites, on my machine (obviously - since it runs at the command line). At the moment, I have run out of ideas.
What is causing the script to run fine at the console, and then fail when run by cron?
[[Relevant Details]]
OS: Linux 16.0.4 LTS
bash: version 4.3.46(1)
cron user: myself (i.e. same user at the command prompt)
dryscrape: version 1.0.1
The solution to this was to call the dryscrape.start_xvfb() method before starting the dryscrape session.
Cron user does not have display, so you cannot run any command which requires a display.
You need to modify the python script to do not use any type of display (check carefully, because some python commands, even though they do not open any display , they internally check for this variable).
The best way to test is to ssh into the machine without Display, and check if you can run it from there without erros.

Ubuntu run python script on system startup that is using Firefox

I wrote python script that uses subprocess.pOpen() module to run and manipulate with 2 GUI programs: Firefox and VLC player. I am using Ubuntu 14.04 LTS operating system in Desktop mode.
My problem is when I try to run that python script when system starts, script is running but Firefox or VLC don't start.
So far, I tried to make shell script to run my python script and then with crontab with #reboot /home/user/startup.sh to execute my python script. I set all permissions for every script that is using. I gave my user root permisions so everything is OK with that.
I also tried to run my script putting command "sudo python /path/to/my/script.py" in /etc/rc.local file but that also is not helping.
I googled and found out people using .desktop files that they put in ~/.config/autostart/ directory but that also failed. Example of what I wrote:
[Desktop Entry]
Type=Application
Exec="sudo python /home/user/path_to_my_script/my_script.py"
X-GNOME-Autostart-enabled=true
Name=screensplayer
Comment=screensplayer
And I saved this as program.desktop in ~/.config/autostart/ directory but it does not work. I am sure there is a way to fix this but don't know how. Any help will be appreciated!
Found solution to my problem. When you are running commands with pOpen in python like this:
FNULL = open(os.devnull, 'w')
_FIREFOX_START = ["sudo", "firefox", "test.com/index.html"]
subprocess.Popen(self._FIREFOX_START, stdout=self.FNULL, stderr=subprocess.STDOUT)
it won't run apps because of "sudo" word, when I removed it, it worked.
Also run gnome-session-properties in terminal and add new startup application, be aware that you have to execute python script without sudo, like this:
python /home/user/path_to_script/script.py
Also, I granted my user root privileges so kepp that in mind.

Categories