2

My python script needs to be killed every hour and after I need to restarted it. I need this to do because it's possible sometimes (I create screenshots) a browser window is hanging because of a user login popup or something.. Anyway. I created 2 files 'reload.py' and 'screenshot.py'. I run reload.py by cronjob.

I thought something like this would work

    # kill process if still running
try :   
        os.system("killall -9 screenshotTaker");
except :
        print 'nothing to kill'

# reload or start process
os.execl("/path/to/script/screenshots.py", "screenshotTaker")

The problem is, and what I read aswel the second argument of execl (the given process name) doesn't work? How can I set a process name for it to make the kill do it's work?

Thanks in advance!

directory
  • 3,093
  • 8
  • 45
  • 85
  • I think that `os.kill` would be better. – aldeb Jun 12 '13 at 15:21
  • possible duplicate of [Is there a way to change effective process name in Python?](http://stackoverflow.com/questions/564695/is-there-a-way-to-change-effective-process-name-in-python) – Piotr Dobrogost Feb 12 '15 at 15:21

1 Answers1

1

The first argument to os.execl is the path to the executable. The remaining arguments are passed to that executable as if their where typed on the command-line.

If you want "screenshotTaker" become the name of the process, that is "screenshots.py" responsibility to do so. Do you do something special in that sense in that script?

BTW, a more common approach is to keep track (in /var/run/ usually) of the PID of the running program. And kill it by PID. This could be done with Python (using os.kill) At system-level, some distribution have helpers for that exact purpose. For example, on Debian there is start-stop-daemon. Here is a excerpt of the man:

start-stop-daemon(8)            dpkg utilities            start-stop-daemon(8)

NAME
       start-stop-daemon - start and stop system daemon programs

SYNOPSIS
       start-stop-daemon [options] command

DESCRIPTION
       start-stop-daemon  is  used  to control the creation and termination of
       system-level  processes.   Using   one   of   the   matching   options,
       start-stop-daemon  can  be  configured  to find existing instances of a
       running process.
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • Thanks for your explanation! I have a database with more than 1 million records with website names. I need to create screenshots of these websites, this happens in 'screenshots.py' taking records from the database and take the shots.. I will use the os.kill thanks for that, but I should set a process name in screenshot.py? How do I do this? I found out that it's not really possible at the moment with python? – directory Jun 12 '13 at 19:05
  • For now, I created a little workaround. I store the pid id from screenshots.py into the mysql database. reload.py takes the pid id from the database and kills/delete it. but setting a process name to kill by would be much cleaner – directory Jun 12 '13 at 19:16