Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch a job in a shell which will persist even if the shell which launches it terminates

How do I lauch a job in a Linux shell, so that even if the shell which launches it terminates, this job will still persist?

More specifically, I am trying to run strace on a process. It runs perfectly if I do it in a terminal. However, I want to do it in a remote shell which will stop as soon as all the commands have been executed. "strace -p pid &" doesn't have any effect because when the shell stops, the background job also gets killed.

How should I do it?

nohup seems to be thing I am looking for. However, ssh user@remote_machine script_name doesn't seem to have any effect neither. In the script I have "nohup strace -p pid"

Thanks a lot!

like image 834
yangsuli Avatar asked Feb 01 '26 19:02

yangsuli


1 Answers

Nohup and Screen can be used to run a command even if the session is disconnected or the user logs out. I use them both, but “Screen” is better.

nohup ./<script_name> &

How to use screen?

creat a task:

$ screen -S task

Execute a command in the task window,if your task not finished, use

$ Ctrl+a+d
to save the task. It will show the following info:
[detached]

if your task has been finished, use “exit” to exit screen:

    $ exit

[screen is terminating]

You can use screen -ls to find any screen info:

$ screen -ls
There is a screen on:
10000.task (Detached)

Use “screen -r” to recover the task:

$ screen -r 10000
like image 53
Satish Avatar answered Feb 03 '26 09:02

Satish