LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2010, 05:55 AM   #1
IanVaughan
Member
 
Registered: Jun 2009
Location: Brighton, UK
Posts: 50

Rep: Reputation: 16
how to use fg foreground command? no such job!


I have many processes running that printf local debug. (they will log to file later!)

I start them by
Code:
( sleep 20000000 | fp ) &
else they get killed/stop by themselves.

So now its running in the background, I want to bring it to the front to see the printfs.

Code:
# pgrep fp
3301

# fg 3301
-bash: fg: 3301: no such job
Why no such job?!
How do I use it?

man fg
Quote:
fg [jobspec]
Resume jobspec in the foreground, and make it the cur-
rent job. If jobspec is not present, the shellâs
notion of the current job is used. The return value
is that of the command placed into the foreground, or
failure if run when job control is disabled or, when
run with job control enabled, if jobspec does not
specify a valid job or jobspec specifies a job that
was started without job control.
What is jobspec?
 
Old 04-16-2010, 05:58 AM   #2
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
Quote:
Originally Posted by IanVaughan View Post
I have many processes running that printf local debug. (they will log to file later!)

I start them by
Code:
( sleep 20000000 | fp ) &
else they get killed/stop by themselves.

So now its running in the background, I want to bring it to the front to see the printfs.

Code:
# pgrep fp
3301

# fg 3301
-bash: fg: 3301: no such job
Why no such job?!
How do I use it?

man fg

What is jobspec?


First run the command jobs to know the job id which is different from the process id.
Then use fg to bring it in foreground:

Quote:
#fg job_id
 
2 members found this post helpful.
Old 04-16-2010, 06:06 AM   #3
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
Quote:
Originally Posted by IanVaughan View Post
I have many processes running that printf local debug. (they will log to file later!)

I start them by
Code:
( sleep 20000000 | fp ) &
else they get killed/stop by themselves.

So now its running in the background, I want to bring it to the front to see the printfs.

Code:
# pgrep fp
3301

# fg 3301
-bash: fg: 3301: no such job
Why no such job?!
How do I use it?

man fg

What is jobspec?
Code:
man jobs
jobs command will display the background process.Then fg <number>
 
1 members found this post helpful.
Old 04-16-2010, 06:17 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by cola View Post
...
Then fg <number>
AFAIR it's

Code:
fg %JOB_NUMBER
- at least, this is how it works for me.

A job can also be put into foreground based on its PID using 'kill' with the appropriate signal - see

Code:
man kill
man 7 signal
.
 
Old 04-16-2010, 06:37 AM   #5
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
Quote:
Originally Posted by Sergei Steshenko View Post
AFAIR it's

Code:
fg %JOB_NUMBER
- at least, this is how it works for me.

A job can also be put into foreground based on its PID using 'kill' with the appropriate signal - see

Code:
man kill
man 7 signal
.
Code:
If Python don't have unless construct, it means it doesn't need one. (c) ghostdog74
Why is this?
 
0 members found this post helpful.
Old 04-16-2010, 07:09 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by cola View Post
Code:
If Python don't have unless construct, it means it doesn't need one. (c) ghostdog74
Why is this?
I quoted ghostdog74 - ask him . Perl does have 'unless' construct, and I like it.
 
0 members found this post helpful.
Old 04-16-2010, 07:15 AM   #7
IanVaughan
Member
 
Registered: Jun 2009
Location: Brighton, UK
Posts: 50

Original Poster
Rep: Reputation: 16
Can you not post crap thats not related to this topic please!

Anyway, jobs returns nothing for me!

Cheers.
 
Old 04-16-2010, 07:19 AM   #8
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
Quote:
Originally Posted by IanVaughan View Post
Can you not post crap thats not related to this topic please!

Anyway, jobs returns nothing for me!

Cheers.
If jobs does not return anything then there is no background process.
 
Old 04-16-2010, 07:22 AM   #9
IanVaughan
Member
 
Registered: Jun 2009
Location: Brighton, UK
Posts: 50

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by cola View Post
If jobs does not return anything then there is no background process.
Interesting!
But I have many processes running, are they actually not all background processes, or just processes then?

What I may be after then is to be able to switch to another process that is running!?
 
Old 04-16-2010, 07:22 AM   #10
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
What actually this does?

have you tried this on a shell ?

Code:
( sleep 20000000 | fp ) &
do something like

Code:
(sleep 10 | ps ) &
What goes in to background ?


Hope it gives you a pointer.

I personally did not like the word crap for normal discussion
 
Old 04-16-2010, 07:29 AM   #11
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
Quote:
Originally Posted by IanVaughan View Post
Interesting!
But I have many processes running, are they actually not all background processes, or just processes then?

What I may be after then is to be able to switch to another process that is running!?
If you use "&" at the end of a command,then you get a background command.
Example:
Code:
mplayer file1.flv &
 
Old 04-16-2010, 07:34 AM   #12
IanVaughan
Member
 
Registered: Jun 2009
Location: Brighton, UK
Posts: 50

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by PMP View Post
What actually this does?

have you tried this on a shell ?

Code:
( sleep 20000000 | fp ) &
do something like

Code:
(sleep 10 | ps ) &
What goes in to background ?
Ok, my mistake, I had killed my "fp" background process!
When started (as quoted above) jobs gives me :-
Code:
[1]+  Running                 ( sleep 20000000 | fp ) &
so, typing in
Code:
fg 1
should bring it to the front, right? I cant prove it right now, but will post back when I can.

And once in the fg, how can I push it back to the bg again?

thanks for bearing with me!
 
Old 04-16-2010, 07:43 AM   #13
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Yes your process fp is running at that time.

My question was it in background ?
pgrep will give you the processid, no matter it is in forground or background.

Have you tried what I said

Code:
(sleep 1000 | ps ) &
after doing so you will receive

-bash-3.2$ (sleep 1000 | ps) &
[5] 30766

Job Number, Processid


no if you do

fg 5 ie fg [job_number] you will se your job in background.
 
Old 04-16-2010, 08:26 AM   #14
tommylovell
Member
 
Registered: Nov 2005
Distribution: Raspbian, Debian, Ubuntu
Posts: 380

Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by IanVaughan View Post
Ok, my mistake, I had killed my "fp" background process!
When started (as quoted above) jobs gives me :-
Code:
[1]+  Running                 ( sleep 20000000 | fp ) &
so, typing in
Code:
fg 1
should bring it to the front, right? I cant prove it right now, but will post back when I can.

And once in the fg, how can I push it back to the bg again?

thanks for bearing with me!
To bring it to the foreground you need to
Code:
fg %1
To put it in the background you need to
Code:
bg %1

e.g.
Code:
[root@athlonz ~]# ( sleep 20000000 | cat /etc/*ease ) &
[1] 31071
[root@athlonz ~]# Fedora release 10 (Cambridge)
Fedora release 10 (Cambridge)
Fedora release 10 (Cambridge)

[root@athlonz ~]# jobs
[1]+  Running                 ( sleep 20000000 | cat /etc/*ease ) &
[root@athlonz ~]# ( sleep 20000000 | cat /etc/fstab ) &
[2] 31120
[root@athlonz ~]# 
#
# /etc/fstab
# Created by anaconda on Mon Feb 15 02:00:36 2010
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or vol_id(8) for more info
#
UUID=54716048-c91b-4f48-b37e-fb09ce21e412 /     ext3    defaults        1 1
UUID=1fd8498a-868b-46b6-8ced-01155b0c5962 /bkup ext3    defaults        1 2
UUID=97bcf411-5558-4cf9-9aef-5108fa686246 /boot ext3    defaults        1 2
UUID=59d3649f-4c9d-46b2-94c8-b7d29578328e /boot2 ext3   defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID=b827e20f-db5b-47b2-be38-17489895a0ad swap  swap    defaults        0 0
/dev/mapper/vgz01-tommy /home/tommy             ext3    defaults        1 2

[root@athlonz ~]# jobs
[1]-  Running                 ( sleep 20000000 | cat /etc/*ease ) &
[2]+  Running                 ( sleep 20000000 | cat /etc/fstab ) &
[root@athlonz ~]# fg %1
( sleep 20000000 | cat /etc/*ease )
^Z
[1]+  Stopped                 ( sleep 20000000 | cat /etc/*ease )
[root@athlonz ~]# jobs
[1]+  Stopped                 ( sleep 20000000 | cat /etc/*ease )
[2]-  Running                 ( sleep 20000000 | cat /etc/fstab ) &
[root@athlonz ~]# bg %1
[1]+ ( sleep 20000000 | cat /etc/*ease ) &
[root@athlonz ~]# jobs
[1]-  Running                 ( sleep 20000000 | cat /etc/*ease ) &
[2]+  Running                 ( sleep 20000000 | cat /etc/fstab ) &
[root@athlonz ~]# kill %1
[root@athlonz ~]# jobs
[1]-  Terminated              ( sleep 20000000 | cat /etc/*ease )
[2]+  Running                 ( sleep 20000000 | cat /etc/fstab ) &
[root@athlonz ~]# 
[root@athlonz ~]# jobs
[2]+  Running                 ( sleep 20000000 | cat /etc/fstab ) &
[root@athlonz ~]# kill %2
[root@athlonz ~]# 
[2]+  Terminated              ( sleep 20000000 | cat /etc/fstab )
[root@athlonz ~]# 
[root@athlonz ~]# jobs
[root@athlonz ~]#

Last edited by tommylovell; 04-16-2010 at 08:37 AM. Reason: more clarification
 
Old 11-02-2016, 03:49 AM   #15
mjuckes
LQ Newbie
 
Registered: Nov 2016
Posts: 1

Rep: Reputation: Disabled
jobs vs processes

There seems to be an important distinction between "jobs" in the current shell and "processes" in the session. "ps" and "kill" can be used to look at and chnage the states of processes in the session (e.g. "kill -CONT $PID" to bring a suspended process back to life). "jobs" and "fg" can only be used for jobs in the current shell.
 
  


Reply

Tags
bg, fg, job, pid



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
all cron job details with any single command mayrus9 SUSE / openSUSE 3 04-01-2010 03:31 AM
Job resumed in background still outputs to stdout in foreground lumix Linux - Newbie 3 11-28-2008 08:05 PM
cron job and boot command Thulemanden DamnSmallLinux 3 11-08-2006 12:22 AM
Cron Job help...how to write this command? pxumsgdxpcvjm Linux - Newbie 2 09-05-2006 04:42 AM
trying to run job with at command mnauta Linux - Networking 1 03-10-2005 04:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 PM.

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