Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-05-2010, 10:59 AM   #1
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Rep: Reputation: 62
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!
Old 01-05-2010, 11:19 AM   #2
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
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.
Old 01-05-2010, 11:23 AM   #3
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
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.
Old 01-05-2010, 11:50 AM   #4
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
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?


[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 ~]#

Last edited by arashi256; 01-05-2010 at 11:52 AM.
Old 01-05-2010, 12:17 PM   #5
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Originally Posted by arashi256 View Post
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.

Originally Posted by arashi256 View Post
[root@joshua11 ~]# fg 1692
-bash: fg: 1692: no such job
[root@joshua11 ~]#
A job number would normally be a small number. For example
[user@machine:~]:xterm &
[1] 8786
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".



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Process management and process control cll975dll General 1 09-04-2009 04:37 PM
How to take control of an orphaned job kav Linux - Software 2 05-18-2007 01:54 PM
Process Table-Process Control Block sarahmencer Linux - General 1 01-09-2006 10:14 PM
Job control King4lex Linux - Newbie 3 08-16-2004 01:50 PM
Unix Job Control in C apydia Programming 3 05-08-2004 12:29 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration