LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-05-2010, 09:59 AM   #1
arashi256
Member
 
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, 10:19 AM   #2
clvic
Member
 
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, 10:23 AM   #3
nuwen52
Member
 
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, 10:50 AM   #4
arashi256
Member
 
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?

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

Last edited by arashi256; 01-05-2010 at 10:52 AM.
 
Old 01-05-2010, 11:17 AM   #5
David1357
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
Quote:
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.

Quote:
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
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".
 
  


Reply

Tags
screen



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 03:37 PM
How to take control of an orphaned job kav Linux - Software 2 05-18-2007 12:54 PM
Process Table-Process Control Block sarahmencer Linux - General 1 01-09-2006 09:14 PM
Job control King4lex Linux - Newbie 3 08-16-2004 12:50 PM
Unix Job Control in C apydia Programming 3 05-08-2004 11:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:23 AM.

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