Ceci est une ancienne révision du document !
Last issue, we looked at using the ‘&’, ‘&&’ and ‘||’ commands as well as one or two other bash quirks. This time around, I want to explain a bit more as my proofreader gave me the “sure… but OK”routine.
When you use the ampersand at the end of a command or shell script, you basically tell it to go play in the park while you continue to type in the terminal. If you don’t, it could hog your terminal. For most of us, this is not a problem, as we all use modern terminal emulators, where we can just open another tab or instance. The issue arises when you are doing something on a remote server via, say ssh, and you have a script hogging the command line, so we use the ampersand at the end to tell it to play in the park (go to the background).
That said, we don’t always have to do it one way only. We can take a look at what’s cooking and put it back in the oven, so to speak. We can have multiple ‘jobs’ running in the background and we can switch between them as they run. Using sleep in a script is usually a good way to stretch it out, as I have no long-running scripts at hand. I have been watching a cartoon with an alien and his robot invading earth, so I made a simple script:
#!/usr/bin/env bash while true; do
echo " Doom, doom, doom!" sleep 3
done
Save it as doom.sh and we can run it with: bash doom.sh and you will notice that you cannot type. You can press CTRL+z to stop it. Unlike pressing CTRL+c, you will see a message, with a job number and what happened. I’ll add a picture, but I urge you to do it yourself:
Remember, stopped does not mean it is dead or cancelled. We have the job number; [1], in my case, and I can tell it to go continue, but outside, in the park, with: bg %1 (the syntax is bg %<job number>) and I should get my invader Zim doom warnings again. However, I can type, say, ls -la - and I will get output (even though I have a doom scroller scrolling along).
We can call the job back into the house (foreground) from the park (background) with a simple: fg %1 - the challenge here is to remember the job number, should you have lots of jobs running - but we can talk about that later. Go ahead and try it, even if your typing looks wonky, don’t worry as the text you are seeing will not execute, only what you have typed. (You can just use just: %1, but it is good to know the proper way.)
Obviously in the real world, you will probably zip a large file on a server and scp it back to your machine, like say, a mongodb database. This may be gigabytes in size and take a while, but it will not have constant output like our stupid doomscroller. Here we would probably just use the ampersand at the end of the command and be done with it.
There is a caveat here; if there is something needing user input, like in the above case, I would need to put a password in to copy to my machine, or else nothing will happen and you will feel a chop, like I did here:
admin@jumphost:~/fcm$ scp mongodb.zip edd@192.168.0.200:/home/ed/fcm &
[1] 5776
Only once you bring it to the foreground, you are asked for the password for the other machine (in my case), and only then will the copying commence and you can type bg to put it back into the background. So don’t immediately put everything to the background because you can.
If you forget the job number, especially if you have multiple jobs running, you can use the ‘jobs’ command to check them out. This is why I always say that you should name your scripts properly, not just z.sh and x.sh and so forth (I still name mine like ‘a.sh’ if I was going to use it temporarily, but it is a bad habit and you shouldn’t do it). When you run the jobs command, you will see the running jobs, as well as the stopped jobs (the ones you pressed CTRL+z on). When you get the running jobs printed to the screen, you may see a ‘+’ and a ‘-’ after the job number. The plus indicates the last job you fiddled with, be it running or stopped, and the minus means the one just before that. Not the previous job mind you, but the previous one you fiddled with, be that running it, or stopping it, or bringing it forward, or sending it backward. The reason this is handy, is that you don’t need to type: bg %3 or fg %3, if it has the plus next to the three, [3]+, as you can then just type bg or fg and it will act on the one with the plus.
Your homework is to make 4 scripts. They don’t need output, they just need to sleep. Then run them and randomly stop 2, with CTRL+z. Now manipulate them (you can do two at a time with bg %1 %2), but type “jobs” after each time, so you can see the plus and minus move. Easy peasy, lemon squeezy. What do you think?
If I made mistakes, please send them to: misc@fullcirclemagazine.org