0

I was trying out the commands as the video of Season1 Episode8 Processes and Jobs progressed. I have a bash terminal running on Ubuntu 16.04.

while true; do echo ping; sleep 1; done
^Z

Instead of getting:

[1]+  Stopped               while true; do echo ping; sleep 1; done

I get:

[1]+  Stopped                 sleep 1

bg%1 further gives only

[1]+ sleep 1 &

instead of a series of ping in 1s interval in the background

Any ideas on why this happens and how to actually get a series of ping in 1s interval in the background would be appreciated.

Aashaka
  • 3
  • 4

5 Answers5

1

Try:

bash <<< 'while true; do echo ping; sleep 1; done'

Result:

^Z
[1]+  Stopped                 bash <<< 'while true; do echo ping; sleep 1; done'

Or using a subshell:

(while true; do echo ping; sleep 1; done)

Result:

^Z
[1]+  Stopped                 ( while true; do
    echo ping; sleep 1;
done )
perreal
  • 94,503
  • 21
  • 155
  • 181
0

Run your command with a & at the end, instead of stopping it. ^Z is too narrow to use with commands like this.

stolenmoment
  • 443
  • 3
  • 6
0

You run the command by either adding an & at the end which is easier you might find more trouble to end the process.

admin1@mysys:~$ while true; do echo ping; sleep 1; done&
[2] 14169
admin1@mysys:~$ ping
ping
ping
^C
admin1@mysys:~$ ping
ping
ping
^C
admin1@mysys:~$ ping
ping
ping
ping
^C
admin1@mysys:~$ ping
kill 14169
admin1@mysys:~$

As you can see, you will have to Cntrl + D or kill the process to stop it.

Another option would be to use 'screen'

Assuming you have screen installed, enter the terminal and execute the command 'screen'

Then you can execute the command:

 while true; do echo ping; sleep 1; done

and then press Cntrl A and then D (keeping Cntrl pressed itself). This will detach you from screen and you can do whatever you want and the command will be executed in the background.

At any time you can list the current screen executing

screen -ls

and then connect to the screen back by executing

screen -r screen_name

This sounds a bit complicated but is a better way to handle things. You can find more details Here

Siju V
  • 143
  • 1
  • 5
  • On sending a `kill -TSTP -pid` for a process with an '&' at its end, the process does suspend (knowledge obtained from @sjsam's answer). Whereas Ctrl-Z gives no result. I wonder what goes on.. – Aashaka Jun 28 '16 at 12:14
  • Multiple answers, including this one, were helpful in increasing my knowledge on this subject. I have upvoted each, and chosen the one best suitable for the case at hand. Thanks for your help :) – Aashaka Jun 28 '16 at 12:17
0

Borrowing from [ this ] answer, Ctrl-Z generates the [ TSTP ] signal to your process and stopping the process is clearly not your intention.

To run a process in the background, do

process  >/dev/null & 
# Here the '>/dev/null' suppresses any output from the command 
# popping up in the screen, this may or may not be desirable
# The & at the end tells bash that the command is to be run in backgroud

For example

$ ping -c 100 192.168.0.1 >/dev/null &
[1] 2849

Note the two numbers [1] & 2849 that bash gave you. The first one is the background process number. Say, if you wish to bring this process to the foregroud, you could use this number

fg 1 # Here fg stands for foreground

The second number is the process ID ie 2849. Say, you wish to terminate the process, you could do it like below :

kill -9 2849 #-9 is for SIGKILL

Edit

In your case, you could wrap the loop inside a function like below

 while_fun() {
 while true
 do
 echo "PING"
 done
 }

and do

while_fun >dev/null &

Or do

while true
 do
 echo "PING" 
 done >/dev/null  &
Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
-1

You could try something like this:

while true; do 
 echo ping 1
 sleep 1
done;

Note that I have only placed semicolon ; on done - marking the end of statement. I tried this on my terminal and behaves as you expect.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32