issue214:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue214:c_c [2025/03/02 08:08] – créée d52fr | issue214:c_c [2025/03/06 10:12] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Last issue, we looked at using the ‘& | + | **Last issue, we looked at using the ‘& |
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). | 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). | ||
Ligne 9: | Ligne 9: | ||
echo " Doom, doom, doom!" | echo " Doom, doom, doom!" | ||
sleep 3 | sleep 3 | ||
+ | done** | ||
+ | |||
+ | Dans le dernier numéro, nous avons étudié l' | ||
+ | |||
+ | Lorsque vous utilisez l' | ||
+ | |||
+ | Cela étant dit, nous ne sommes pas toujours obligés de le faire uniquement d'une seule manière. Nous pouvons jeter un œil à ce qui est en train de cuire et le remettre au four, pour ainsi dire. Nous pouvons avoir plusieurs « tâches » en cours d’exécution en arrière-plan et nous pouvons basculer entre elles au fur et à mesure de leur exécution. Utiliser sleep dans un script est généralement un bon moyen de l’étirer, | ||
+ | |||
+ | # | ||
+ | while true; do | ||
+ | echo " Doom, doom, doom!" | ||
+ | sleep 3 | ||
done | 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: | + | |
+ | **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). | 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.) | + | 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. | + | Enregistrez-le sous le nom doom.sh et nous pourrons l' |
+ | |||
+ | N' | ||
+ | |||
+ | Nous pouvons rappeler la tâche dans la maison (premier plan) depuis le parc (arrière-plan) avec un simple : fg %1 - le défi ici est de se souvenir du numéro de la tâche, si vous avez beaucoup de tâches en cours d' | ||
+ | |||
+ | |||
+ | **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: | 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: | ||
Ligne 26: | Ligne 46: | ||
[1] 5776 | [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. | + | 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, | + | Évidemment, |
+ | |||
+ | Il y a une mise en garde ici : si quelque chose nécessite une saisie de l' | ||
+ | |||
+ | admin@jumphost: | ||
+ | |||
+ | [1] 5776 | ||
+ | |||
+ | Ce n'est qu'une fois que vous l'avez mis au premier plan que l'on vous demande le mot de passe de l' | ||
+ | |||
+ | |||
+ | **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, | ||
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? | 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 | + | If I made mistakes, please send them to: misc@fullcirclemagazine.org** |
+ | |||
+ | Si vous oubliez le numéro d'une tâche, surtout si vous avez plusieurs tâches en cours d’exécution, | ||
+ | |||
+ | Votre devoir consiste à créer 4 scripts. Ils n'ont pas besoin de sortie, ils ont juste besoin de dormir. Ensuite, exécutez-les et arrêtez-en 2 au hasard, avec CTRL+z. Maintenant, manipulez-les (vous pouvez en faire deux à la fois avec bg %1 %2), mais tapez « jobs » après chaque fois, afin de pouvoir voir le plus et le moins se déplacer. Facile comme bonjour. Qu'en pensez-vous ? | ||
+ | |||
+ | Si j'ai fait des erreurs, veuillez les envoyer à : misc@fullcirclemagazine.org |
issue214/c_c.1740899333.txt.gz · Dernière modification : 2025/03/02 08:08 de d52fr