LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Process / Job control. (https://www.linuxquestions.org/questions/linux-newbie-8/process-job-control-780013/)

arashi256 01-05-2010 09:59 AM

Process / Job control.
 
I have a script which runs: -

e2fsck -p -v -c -f /dev/sdb1 > e2fsck_sdb.log &

However, although this runs successfully in the background, I'd like to be able to log off and return later, pull it to the foreground and see how it's doing. When running this, I don't get a job number - it simply returns to the prompt - and using "jobs" returns nothing, although I can see it running by using "ps". I assume this has somehow got divorced from the shell session, so I am unable to use "fg" to bring it back to foreground.

Since I can get the Process ID very easily, is there a way to pull a process to the foreground from it's PID rather than it's job number? Or can I somehow get the correct job number from the PID in order to use "fg"?

Thanks for any help!

clvic 01-05-2010 10:19 AM

Use the command "screen", that can "emulate" terminals on your host.
Basic usage: run "screen", then run your program without the '&'.
Then, use C-a C-d to "detach" the screen session. When you want to reattach to it, run "screen -R". Look at the man page, "screen" is a very flexible command.

nuwen52 01-05-2010 10:23 AM

Why bring it to the foreground?

All the output is being sent to a file. So, do a "cat" on the output file. If you want to watch the output as it goes, you could do "tail -f" on the file. I don't know of a way to bring back a process to the foreground with a PID, so this is the best I can come up with. Or, you could "screen" the command. That way, you can use a "screen -r" to recover the session.

arashi256 01-05-2010 10:50 AM

Screen looks interesting - I'll get to that. However, the main problem seems to be (with any background job, it seems) that once you've logged out and log back in again, you've lost your shell association and although ps shows the process, you can no longer get to it with fg or jobs. Must be a way around that, surely? Or is that what screen is for?

E.G.

[root@joshua11 ~]# ps -elf | grep e2fsck
0 S root 1692 1 0 80 0 - 33038 pipe_w 16:39 ? 00:00:01 e2fsck -c -v -f /dev/sdb1
0 S root 1743 1704 0 80 0 - 25682 pipe_w 16:51 pts/1 00:00:00 grep e2fsck
[root@joshua11 ~]# fg 1692
-bash: fg: 1692: no such job
[root@joshua11 ~]#

David1357 01-05-2010 11:17 AM

Quote:

Originally Posted by arashi256 (Post 3815076)
Must be a way around that, surely? Or is that what screen is for?

Yes, that is what screen is for. When you exit your shell, you lose access to your job list.

Quote:

Originally Posted by arashi256 (Post 3815076)
[root@joshua11 ~]# fg 1692
-bash: fg: 1692: no such job
[root@joshua11 ~]#

A job number would normally be a small number. For example
Code:

[user@machine:~]:xterm &
[1] 8786
[user@machine:~]:

Here, the job number is "1". The PID is "8786". Normally, you would bring this job to the foreground using "fg %1".

For more information, run "man bash" and read the section titled "JOB CONTROL".


All times are GMT -5. The time now is 09:54 PM.