Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a Shell Script which I can execute which will Execute a Program that needs to run for infinite amount of time until I choose to kill it.

I am already tried nohup, screen, disown (in netcat /bin/sh Shell) but they all failed to do the job.

The Program which needs to run also needs arguments.

EDIT: Comments asked me to mention the Problems with the methods I tried.

nohup: Closing the NetCat window also closes the Process

bg: At the start NetCat reports : "No Job Control in this session"

disown: Same result as nohup

screen: "Must be connected to a Terminal"

share|improve this question
Running a program in screen, tmux or with nohup & should all work. You already mentioned most of these. Why did these not work? Which problems did you run into ? – Hennes Aug 28 '13 at 19:34
Added the Problems with the used commands. – John Aug 28 '13 at 19:40
How did you try to use screen? Once you're in a screen session you are in a terminal. If you tried to execute the command directly from the command line i.e. % screen myCommand then you're not going to have a controlling terminal. Just type screen, run your command after you get a new prompt. You can then close the window and your screen session will continue. – skarface Aug 28 '13 at 20:34 This seems to be someone trying to do the same thing. – skarface Aug 28 '13 at 20:46

Replace "bash" with whatever you need to run. will not survive reboots.

perl -e "if(fork){exit}else{exec(\"bash\")}"

edit: You can print $$ to a file in the else, prior to the exec if you want to capture the pid. You can also just grep through PS output to kill based on pid.

share|improve this answer
From the OP: "needs to run for infinite amount of time until I choose to kill it" – not sure if a reboot is included there. – slhck Aug 28 '13 at 19:46
then add the above to your boot scripts. there's very little that will automatically survive a reboot. nohup/screen/etc surely don't. – skarface Aug 28 '13 at 19:49
I just need it to run as long as the system is up. – John Aug 28 '13 at 19:50
Oh, I think you misunderstood. I was just thinking about the "how to go about killing it" part. – slhck Aug 28 '13 at 19:50
I think I should be able to kill it using kill -9 PID – John Aug 28 '13 at 19:54

First of all, a shell script can not prevent some (background) process from terminating if the OS decides to kill it or if the process dies. With that said, here is a suggestion on how to restart a service. It is crude, but simple.

Create a "semaphore" file and use that to break a forever-loop that restarts some job.


function run {
    local t=`mktemp  /tmp/service.XXX`
    touch $t
    while [ -f $t ]; do
        # restart a job here, e.g
        nc -l -p 4444 & pid=$!
        echo $pid > $t
        wait $pid

function stop {
    local pf=`ls /tmp/service.*| wc -l`
    if [ $pf -gt 1 ]; then
        echo why are there several pid files?

    elsif [ $pf -lt 1 ]; 
        echo nothing to stop

        pid=`cat /tmp/service.*`
        rm -f /tmp/service.*
        kill $pid

Source the file in your shell and run the functions. The script may contain errors, but the idea should be usable.

share|improve this answer
I think the OP is asking for something that will stay running even once the controlling session dies. The above will make something resilient to being killed, but it won't stop a process from dying when the controlling session goes away. You need to fork somewhere. – skarface Aug 28 '13 at 20:37
You're right. I forgot to mention that it should run in a screen. – Ярослав Рахматуллин Aug 28 '13 at 20:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .