running python script as startup service ubuntu 12.04 - python

My python script takes multiple directories as input from user and I want that the user should input the directories for once only and then the program should run continuously over the same directories even after the system boots without asking the user again. I want to use Supervisor configuration to set this up. Any help ??

Make a cron-job or create a service init script (see the manual for how to write a init-script under the current Ubuntu version).
Doing a cronjob:
EDITOR=nano; crontab -e
#reboot cd /home/user/place_where_script_is; python3 myscript.py param1 param2
There's no efficient way for the user to input data to startup script, mainly because they run in a root environment.
A cron-job always runs in the environment where you invoked crontab -e (hopefully the users environment) but even then..
You can not interact with it because it's run in a separate "shell".
Unix socket?
In your script, add a listening socket on a unix socket.
in your .bashrc script (not sure where Ubuntu has it's post X startup scripts), invoke callMyScript.py that connects to the unix socket and sends instructions there.
This way you can interact with the cronjob/service script.
Here's how you keep track if a service is dead or not
PID files is the key:
#!/usr/bin/python3
pidfile = '/var/run/MyApplication.pid'
def pid_exists(pid):
"""Check whether pid exists in the current process table."""
if pid < 0:
return False
try:
os.kill(pid, 0)
except OSError, e:
return e.errno == errno.EPERM
else:
return True
if isfile(pidfile):
with open(pidfile) as fh:
thepid = fh.read()
pidnr = int(thepid)
if pid_exists(pidnr):
exit(1) # The previous instance is still running
else:
remove(pidfile) # Prev instance is dead, remove pidfile
# Create a pid-file with the active PID written in it
with open(pidfile, 'w') as fh:
fh.write(str(getpid()))
## Your code goes here...
remove(pidfile)
This way you can convert your Cron job to look like:
EDITOR=nano; crontab -e
* * * */1 cd /home/user/place_where_script_is; python3 myscript.py param1 param2
Which will run the script each minute, and if the script is dead or not started the script will start up again.
Same goes for service status-all myapp if you've written a init script, the init script will check the PID file (you'd have to write this in your init script and check the PID yourself much like the above Python code) and see if the process is dead.

You can add it to your crontab
crontab -e
Add the following line, which will get executed when your computer boot up:
#reboot python /path/to/your/script.py
Make sure have at least one empty line at the end.
As to restarting from where your script left off, you have to program that into your application logic.

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!

What is neccesary to daemonize a python script?

I have a python script that needs to run as a daemon on startup. The process detaches from tty(and pdb), but the code doesn't run.
I've narrowed it down to a minimal example
import daemon
from time import sleep
f1 = open('out.txt','a')
with daemon.DaemonContext():
while(1):
f1.write('this is a test')
sleep(5)
I expect the script to keep runiing and adding a line to out.txt every 5 seconds, but the script just detaches from tty(or pdb) and ps -ax shows that the python interpreter isn't running anymore. out.txt is created, but stays empty
You may want to use a process supervisor.
To simplify the process and have a portable solution not depending for example on systemd (Linux only) you could install for example immortal, in FreeBSD just need to do:
pkg install immortal
Then create a your-script.yml with something like this:
cmd: sleep 3
And daemonize it with:
$ immortal -c test.yml
To check the status you could use immortalctl:
$ immortalctl
PID Up Down Name CMD
29993 0.0s test sleep 3
If want to have it always up even when rebooting, just move your script (in FreeBSD) to /usr/local/etc/immortal/your-script.yml, check more about immortaldir
You can add more option for exaple:
cmd: iostat 3
log:
file: /tmp/iostat.log
age: 10 # seconds
num: 7 # int
size: 1 # MegaBytes
require_cmd: test -f /tmp/foo
For more examples check: https://immortal.run/post/run.yml/

Notify user while python running in background

So i have a code that i'm trying to run and when it finish it should display a message to the user saying what changes have happend. This works fine if i run it on terminal. But when i add it to a cronjob nothing happens and no error is shown.
It seems to me that it is losing the display session, but cant figure it how to solve it. Here is the show message code:
def sendmessage(message):
subprocess.Popen(['notify-send', message])
return
I also tried this version:
def sendmessage(message):
Notify.init("Changes")
n = Notify.Notification.new(message)
n.show()
return
Which gives the error (only in background also):
cannot open display:
Activated service 'org.freedesktop.Notifications' failed: Process org.freedesktop.Notifications exited with status
Would be very thankful for any help or alternative given.
I had a similar issue running a system daemon, where I wanted the user to be notified about a network bandwidth upload being exceeded.
The program was designed to run under systemd but at a push also under upstart.
You may get some mileage out of my configuration files:
systemd - bandwidth.service file
[Unit]
Description=Bandwidth traffic monitor
Documentation=man:bandwidth(7)
After=graphical.target
[Service]
Type=simple
Environment="DISPLAY=:0" "XAUTHORITY=/home/#USER#/.Xauthority"
PIDFile=/var/run/bandwidth.pid
ExecStart=/usr/bin/dbus-launch /usr/sbin/bandwidth_logd
ExecReload=/bin/kill -HUP $MAINPID
User=root
[Install]
WantedBy=graphical.target
upstart - bandwidth.conf file
#
# These are the scripts that run when a network appears.
# Use this on an upstart system in /etc/init
# test for systemd or upstart system with
# ps -p1 | grep systemd && echo systemd || echo upstart
# better
# ps -p1 | grep systemd >/dev/null && echo systemd || echo upstart
# Using upstart the script will need to daemonise which is a bugger
# so test for it and import Daemon_server.py
description "Bandwidth upstart events"
start on net-device-up # Start a daemon or run a script
stop on net-device-down # (Optional) Stop a daemon, scripts already self-terminate.
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
#Set environment variables
env DISPLAY=":0"
export DISPLAY
env XAUTHORITY="/home/#USER#/.Xauthority"
export XAUTHORITY
script
# You can put shell script in here, including if/then and tests.
# replace #USER# with your name and ensure that you have .Xauthority in $HOME
/usr/sbin/bandwidth_logd
end script
You will note that in both configuration files the environment becomes key and #USER# is replaced by a real user name with a valid .Xauthority file in their $HOME directory.
In the python code I use the following to emit the message(import notify2).
def warning(msg):
result = True
try:
notify2.init("Bandwidth")
mess = notify2.Notification("Bandwidth",msg,'/usr/share/bandwidth/bandwidth.png')
mess.set_urgency(2)
mess.set_timeout(0)
mess.show()
except:
result = False
return result

Why won't this Python script run as a startup application in Ubuntu 12.04?

I've written this watchdog script to monitor VLC player and kill it when playback has stopped because VLC continues to inhibit the power management daemon after playback. The script works. I can run it from the command line or through IDLE and it kills VLC when playback stops. I've added many variations of the command to start the script to my startup applications as described here but when I reboot, if it is running at all, it stops as soon as VLC starts. Restarting it from a terminal cause it to stay running and do what it is supposed to do. I don't know if this is a problem with the script or something peculiar about Ubuntu Startup Applications (although I'm leaning towards Ubuntu). Maybe something to do with permissions? (Although I did chmod +x) Should I be executing some other commands to make sure DBus is up before I launch the script? Part of me thinks that something isn't fully loaded when the script starts so I tried sleeping before launching using the *nix sleep command, the X-GNOME-Autostart-Delay, and time.sleep(n) in the python code. The pythonic way seems to have the best chance of success. The *nix ways seem to only make startup take longer and at the end of it I find that the process isn't even running. I'm using the python-setproctitle module to name the process so I can quickly see if it is running with a ps -e from terminal. I'm out of ideas and about ready to just manually run the script whenever I reboot (although in principle I think that the machine should do it for me because I told it to). Some variations of Startup Application command lines that I've tried are:
/path/to/script/vlc_watchdog.py
"/path/to/script/vlc_watchdog.py"
/path/to/script/vlc_watchdog.py &
"/path/to/script/vlc_watchdog.py &"
python /path/to/script/vlc_watchdog.py
python /path/to/script/vlc_watchdog.py &
"python /path/to/script/vlc_watchdog.py"
"python /path/to/script/vlc_watchdog.py &"
bash -c "/path/to/script/vlc_watchdog.py"
sleep 30 ; /path/to/script/vlc_watchdog.py
sleep 30 && /path/to/script/vlc_watchdog.py
etc...
Full script:
#!/usr/bin/env python
import time
time.sleep(30)
import dbus
import os
import subprocess
from subprocess import Popen, PIPE
import daemon
import setproctitle
setproctitle.setproctitle('VLC-Watchdog')
sleeptime = 5
def vlc_killer():
bus = dbus.SessionBus()
vlc_media_player_obj = bus.get_object("org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2")
props_iface = dbus.Interface(vlc_media_player_obj, 'org.freedesktop.DBus.Properties')
pb_stat = props_iface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
if pb_stat == 'Stopped':
os.system("kill -9 $(pidof vlc)")
else:
time.sleep(sleeptime)
def vlc_is_running():
ps = subprocess.Popen(['ps', '-e'], stdout = PIPE)
out, err = ps.communicate()
for line in out.splitlines():
if 'vlc' in line:
return True
return False
def run():
while True:
if vlc_is_running():
vlc_killer()
else:
time.sleep(sleeptime)
with daemon.DaemonContext():
run()
In the shell script that starts your Python code (the one in the Ubuntu startup/initialization process), use something like:
#!/bin/sh
set -x
exec > /tmp/errors.out 2>&1
/path/to/script/vlc_watchdog.py
Then after things go awry again (that is, after another reboot), inspect /tmp/errors.out to see the error messages related to whatever happened. There should be a Python traceback in there, or at least a shell error.

Using cron to start a python script at every boot

I tried using cron to start python script , at boot (as mentioned in the link).Running a python script At Boot using cron
I made a python script "hello.py":
#!usr/bin/env python
import time
print "Hello World!"
time.sleep(10)
Then chmod +x hello.py.
I checked,if it running or not,it is giving me output.
I used crontab -e command and added the line #reboot python /home/pi/hello.py &.
Reboot using sudo reboot , but nothing happened! I am newbie .Although I read many discussions but I am not able to fix that!
Generally when I want to verify whether a cronjob ran or not, I redirect all output to a log file like so:
12 0 * * * python /Path/To/File.py > /Path/ToLog/log 2>&1
Then you can check this log timestamp and for its contents

Categories

Resources