Nicolás_Tsu
Nicolás_Tsu

Reputation: 314

How to bring a nohup task to foreground after the shell is closed

I executed the command below in a linux shell:

$ nohup run &

Here run is a blocking command. Then I close the shell and start a new one. However in the new shell I cannot bring the task to foreground via fg, nor can I see it via jobs. Is there still a way to bring it to foreground?

Upvotes: 1

Views: 2476

Answers (3)

Enes Karabacak
Enes Karabacak

Reputation: 1

have the same issue a lot. If you want to kill it, what I usually do is

top | grep {command_name} | grep {user_name}

and if you wait, the background process would show up. The very first column is pid so you can

kill -9 {pid}

Beware it also greps all other commands and stats that includes string "run", so you should carefully check if it is what you really want. ( Command name is usually on the last column )

For example, assume you are using AWS AMI, which default user name is ec2-user

top | grep run | grep ec2-user

gets output like

371 ec2-user  20   0  110m 2668 2664 S  0.0  0.0   0:00.00 run-helper.sh                                                                                                                                

run-helper.sh is not the "run" command, so you should not kill 371

Upvotes: 0

techno
techno

Reputation: 535

Use screen instead of nohup:

screen -dmS demo bash -c 'while ! read -t 1;do echo $((i++));done'

Note: there are no &.

Then exit... Later, you could:

screen -x demo

Hit keys Ctrl+a, then d to leave console running

Or

gnome-terminal -e 'screen -x demo'

Then, simply close window to leave process running.

Upvotes: 1

Yatogami
Yatogami

Reputation: 99

From what I understand, run is a subprocess of the shell, and when the shell closes, run also closes. Maybe using tmux or screen can achieve what you want.

Upvotes: 0

Related Questions