LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Move a command to background. (https://www.linuxquestions.org/questions/linux-newbie-8/move-a-command-to-background-4175613115/)

hack3rcon 09-02-2017 08:44 AM

Move a command to background.
 
Hello.
I like to move a command to background and I found "bg" and "fg" commands but I like when I move command to background then its running and not show me the result suddenly when its finished. For exmaple:
Code:

$ sudo nmap -A IP
$ bg
[1]+ sudo nmap -A IP &

In above example, when the nmap command finished then it show the result in the terminal suddenly.

Thank you.

wpeckham 09-02-2017 10:49 AM

Quote:

Originally Posted by hack3rcon (Post 5754854)
Hello.
I like to move a command to background and I found "bg" and "fg" commands but I like when I move command to background then its running and not show me the result suddenly when its finished. For exmaple:
Code:

$ sudo nmap -A IP
$ bg
[1]+ sudo nmap -A IP &

In above example, when the nmap command finished then it show the result in the terminal suddenly.

Thank you.

That is not quite how background works. You could background it using the command-line
Code:

sudo nmap -A IP &
and get an effect something like that. Several of the other ways to background detach the process from the terminal so you need to redirect the output.

To use bg and fg you need to run the process, then CRTL-Z to stop the process and regain control, then bg will push it into the background. fg would recover it to the foreground. Easier to just start it in the background if that is your intent (or use screen).

All of this information and more is readily available at dozens of sites online. a search with duckduckgo, google, bing, or evn yahoo search would turn up tutorials and references.

MadeInGermany 09-02-2017 11:37 AM

Once a command is started in the foreground you have to pause it with Ctrl-Z,
then you can continue it with bg (or fg) command.

!!! 09-02-2017 10:52 PM

OP asking: "NOT show me the result suddenly when its finished"
 
set +m
https://superuser.com/questions/3059...inishes-execut
That's for just the "done" msg; redirecting output > is a different beast.

Web-researching is always best, but coming up with just the right keywords is the trick!!!

pan64 09-03-2017 04:33 AM

I do not really understand what do you want to do with your command if you do not want to see the result.
By default nmap scans the net and reports the result. If you suppress the result you will lost it.
You ought to read about redirection to save the output (see man bash).
(or probably I misunderstood something?)

hack3rcon 09-03-2017 05:46 AM

Quote:

Originally Posted by wpeckham (Post 5754919)
That is not quite how background works. You could background it using the command-line
Code:

sudo nmap -A IP &
and get an effect something like that. Several of the other ways to background detach the process from the terminal so you need to redirect the output.

To use bg and fg you need to run the process, then CRTL-Z to stop the process and regain control, then bg will push it into the background. fg would recover it to the foreground. Easier to just start it in the background if that is your intent (or use screen).

All of this information and more is readily available at dozens of sites online. a search with duckduckgo, google, bing, or evn yahoo search would turn up tutorials and references.

Excuse me, I forgot to show the full command:
Code:

$ sudo nmap -A
Starting Nmap 6.47 ( http://nmap.org ) at 2017-09-02 15:06 PDT
^Z
[1]+  Stopped                sudo nmap -A IP

When I did "command &" then it cause some problem. for example, run "ping google.com &".

wpeckham 09-03-2017 05:58 AM

#1 ping has no output until it ends. If you used
Code:

ping -c 3 google.com &
so that it terminates after three packets and you would get the output when ping exits. The more general case would require you to redirect STDOUT and STDERR and examine them later.

The best case is to use something like screen (I like to handle it with screenie) so my command is running in a different virtual terminal session and I can go look at the output at my leisure. Is there anything that prevents you from using such a solution?

hack3rcon 09-03-2017 06:11 AM

Quote:

Originally Posted by pan64 (Post 5755112)
I do not really understand what do you want to do with your command if you do not want to see the result.
By default nmap scans the net and reports the result. If you suppress the result you will lost it.
You ought to read about redirection to save the output (see man bash).
(or probably I misunderstood something?)

I need something like switch. when the nmap finished then I switch to it and see the result.

hack3rcon 09-03-2017 06:12 AM

Quote:

Originally Posted by wpeckham (Post 5755121)
#1 ping has no output until it ends. If you used
Code:

ping -c 3 google.com &
so that it terminates after three packets and you would get the output when ping exits. The more general case would require you to redirect STDOUT and STDERR and examine them later.

The best case is to use something like screen (I like to handle it with screenie) so my command is running in a different virtual terminal session and I can go look at the output at my leisure. Is there anything that prevents you from using such a solution?

"ping" command is a problem and I see the result and even can't stop it. I used "kill" command for stop it :(

ondoho 09-03-2017 08:52 AM

i think op really needs several terminal windows.
or if no gui, screen or tmux.

pan64 09-03-2017 10:59 AM

Quote:

Originally Posted by hack3rcon (Post 5755124)
I need something like switch. when the nmap finished then I switch to it and see the result.

I would suggest you the tool named screen (as it was already told). Or just use several terminals instead of one.

wpeckham 09-03-2017 08:26 PM

Quote:

Originally Posted by hack3rcon (Post 5755125)
"ping" command is a problem and I see the result and even can't stop it. I used "kill" command for stop it :(

Use the -c option to limit the number of iterations to make it terminate, or send it CTRL-C to interrupt it and cause exit.

You really should investigate screen and screenie. I think you will like them a LOT.

hack3rcon 09-06-2017 01:30 AM

Quote:

Originally Posted by pan64 (Post 5755201)
I would suggest you the tool named screen (as it was already told). Or just use several terminals instead of one.

"screen" is a good tool and I used it some years ago.


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