This is very nooby question I belive and I've tried everything to avoid asking it here, but I didn't found answere anywhere on whole internet.

Go on and give me downvote but please answer me how to keep execution of long-time-running-required bash script when aborted via CTRL+X or CTRL+C?

  • I want to execute command $ sh myscript.sh and to free up my terminal by CTRL+X / CTRL+C, script should continue to run next 10-20 min as required.

  • I want to execute php script like $ php /path/to/myphpscript.php and to free up my terminal by CTRL+X / CTRL+C, script should continue to run next 2 hours as required.

How to have that?

Cronjob is not my solution.

migrated from serverfault.com Feb 6 '15 at 7:47

This question came from our site for system and network administrators.

  • Why not appending & to run it in the background? This will automatically release your terminal! – Khaled Feb 5 '15 at 10:22
  • @Khaled and how? Please explain me in example $ php /path/to/myphpscript.php – Miloš Đakonović Feb 5 '15 at 10:32
  • You can 'free up' your terminal by running tmux, to have multiple windows and split panes, etc. – Halfgaar Feb 5 '15 at 10:40

Once your terminate a job with CTRL+C it is terminated and you can't tell a dead job to continue and pick up where it was.

The correct term is to run a job in the background, which you can do beforehand:

 ./script &

You can use that in combination with nohup to make the process immune to hang-ups, it will continue to run even if you log out from your bash session:

 nohup ./script &

If the script is already running you can suspend a foreground job with CTRL+Z and instruct it to continue in the background with bg

There's much more in the chapter on job control from the bash manual.

  • 1
    Note that backgrounding a task using bg does not prevent it from being killed by logging out/closing that terminal: to do that, do a disown -h %1 (where 1 is the job id). – shearn89 Feb 5 '15 at 10:57

You can use screen utility. from man page:

DESCRIPTION Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.