Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7439403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:42:02+00:00 2026-05-29T10:42:02+00:00

I currently have a Python webserver application running and I would like to use

  • 0

I currently have a Python webserver application running and I would like to use a python script for cron job to check if the application is running, else start the application.

I have the following code as below:

import os

string = os.popen('ps aux')
n = 0
for line in string.readlines():
  if "python webserver.py" in line:
    n+=1
if n < 1:
  os.system('python webserver.py')

Now the script has executed the application. But what I want is for the script to start the application and exit, leaving the application running. Is there any ways to do so? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:42:03+00:00Added an answer on May 29, 2026 at 10:42 am

    I like to do this with a shell script similar to the following. It is simple and has low likelihood of errors. Generally I put this in a while loop with sleep at the end, but cron works fine too.

    if [ -x myproc.pid ] ; then
        pid=`cat myproc.pid`
    else
        pid=1
    fi
    kill -0 $pid #check if process is still running
    

    As you can see it requires your process to write its PID into a file when it starts up. This avoids problems with grepping ps output because it can be tricky to get a regular expression that always works no matter what new process names come onto your server in future. The PID file is dead certain.

    kill -0 is a little known way of checking if a process is running and is simpler than checking for a directory in /proc. The line with pid=1 is there because the checker is not root therefore the kill check will always fail if you can’t send signals to the process. That is there to cover the first time that myproc is run on this server. Note that this means that the checking script must not run as root (generally good practice) but must run as the same user which runs the process you are checking.

    If you really want to do this in Python, then the os module has access to PIDs and has a kill method. However it doesn’t return a shell exitcode, instead os.kill throws OSError exceptions which you can handle in a try – catch block. The same principle applies of using signal 0 to detect whether the process exists or not but there is a slim chance that your process has died and another process now has the same pid, so do handle that case in your try – catch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application (currently written in Python as we iron out the specifics
I have started to learn about python and is currently reading through a script
I currently have some python code I'd like to port to C++ as it's
I currently have a small Python script that I'm using to spawn multiple executables,
I would like to have a python implementation of a musical instrument library (for
I am new to Python and currently have to write a python program/script to
I currently have Python 2.6.2 installed on my mac. I am writing a script
I have a python subprocess call which I would like to link up to
I currently have a python/cherrypy application where the first page served-up is index.html, which
I currently have both Python 2.6 and 2.7 running on my Linux machine. Now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.