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 167849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:20:54+00:00 2026-05-11T12:20:54+00:00

I have a python script that’ll be checking a queue and performing an action

  • 0

I have a python script that’ll be checking a queue and performing an action on each item:

# checkqueue.py while True:   check_queue()   do_something() 

How do I write a bash script that will check if it’s running, and if not, start it. Roughly the following pseudo code (or maybe it should do something like ps | grep?):

# keepalivescript.sh if processidfile exists:   if processid is running:      exit, all ok  run checkqueue.py write processid to processidfile 

I’ll call that from a crontab:

# crontab */5 * * * * /path/to/keepalivescript.sh 
  • 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. 2026-05-11T12:20:55+00:00Added an answer on May 11, 2026 at 12:20 pm

    Avoid PID-files, crons, or anything else that tries to evaluate processes that aren’t their children.

    There is a very good reason why in UNIX, you can ONLY wait on your children. Any method (ps parsing, pgrep, storing a PID, …) that tries to work around that is flawed and has gaping holes in it. Just say no.

    Instead you need the process that monitors your process to be the process’ parent. What does this mean? It means only the process that starts your process can reliably wait for it to end. In bash, this is absolutely trivial.

    until myserver; do     echo "Server 'myserver' crashed with exit code $?.  Respawning.." >&2     sleep 1 done 

    Or to be able to stop it:

    trap 'kill $(jobs -p)' EXIT; until myserver & wait; do     echo "ldap proxy crashed with exit code $?. Respawning.." >&2     sleep 1 done 

    The above piece of bash code runs myserver in an until loop. The first line starts myserver and waits for it to end. When it ends, until checks its exit status. If the exit status is 0, it means it ended gracefully (which means you asked it to shut down somehow, and it did so successfully). In that case we don’t want to restart it (we just asked it to shut down!). If the exit status is not 0, until will run the loop body, which emits an error message on STDERR and restarts the loop (back to line 1) after 1 second.

    Why do we wait a second? Because if something’s wrong with the startup sequence of myserver and it crashes immediately, you’ll have a very intensive loop of constant restarting and crashing on your hands. The sleep 1 takes away the strain from that.

    Now all you need to do is start this bash script (asynchronously, probably), and it will monitor myserver and restart it as necessary. If you want to start the monitor on boot (making the server "survive" reboots), you can schedule it in your user’s cron(1) with an @reboot rule. Open your cron rules with crontab:

    crontab -e 

    Then add a rule to start your monitor script:

    @reboot /usr/local/bin/myservermonitor 

    Alternatively; look at inittab(5) and /etc/inittab. You can add a line in there to have myserver start at a certain init level and be respawned automatically.


    Edit.

    Let me add some information on why not to use PID files. While they are very popular; they are also very flawed and there’s no reason why you wouldn’t just do it the correct way.

    Consider this:

    1. PID recycling (killing the wrong process):

      • /etc/init.d/foo start: start foo, write foo‘s PID to /var/run/foo.pid
      • A while later: foo dies somehow.
      • A while later: any random process that starts (call it bar) takes a random PID, imagine it taking foo‘s old PID.
      • You notice foo‘s gone: /etc/init.d/foo/restart reads /var/run/foo.pid, checks to see if it’s still alive, finds bar, thinks it’s foo, kills it, starts a new foo.
    2. PID files go stale. You need over-complicated (or should I say, non-trivial) logic to check whether the PID file is stale, and any such logic is again vulnerable to 1..

    3. What if you don’t even have write access or are in a read-only environment?

    4. It’s pointless overcomplication; see how simple my example above is. No need to complicate that, at all.

    See also: Are PID-files still flawed when doing it 'right'?

    By the way; even worse than PID files is parsing ps! Don’t ever do this.

    1. ps is very unportable. While you find it on almost every UNIX system; its arguments vary greatly if you want non-standard output. And standard output is ONLY for human consumption, not for scripted parsing!
    2. Parsing ps leads to a LOT of false positives. Take the ps aux | grep PID example, and now imagine someone starting a process with a number somewhere as argument that happens to be the same as the PID you stared your daemon with! Imagine two people starting an X session and you grepping for X to kill yours. It’s just all kinds of bad.

    If you don’t want to manage the process yourself; there are some perfectly good systems out there that will act as monitor for your processes. Look into runit, for example.

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

Sidebar

Ask A Question

Stats

  • Questions 202k
  • Answers 202k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you want to check out ncurses (or *curses,… May 12, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer The default template for a navigation view controller should do… May 12, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer You can set the DataContext for your control, form, etc.… May 12, 2026 at 8:22 pm

Related Questions

I have a python script that analyzes a set of error messages and checks
I have a Python script that needs to execute an external program, but for
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
I have a python script that I would like to add a Shutdown when
I have a python script that has to launch a shell command for every

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.