Outils pour utilisateurs

Outils du site


issue210:c_c

Ceci est une ancienne révision du document !


Sorry guys, been spending the month in the hospital, so I’m doing this month’s command and conquer from my other computer. This means I cannot remember where we were precisely, so rather than re-doing something we have done, or skipping something, let’s skip to another part of the LPIC exam people have issues with. Cron So if you ever typed ‘man cron’, you may be bombarded with information, and tried to automate something in Ubuntu. Aaaaand found it did not work. This is because Ubuntu has a slightly different take on cron. That said, the principles are still the same. If you look in your /etc folder, you will notice that Ubuntu has more than one folder named cron. Type cd /erc/cron. - and tab to complete to see them. Cron runs only when the computer does, so if you close your laptop and it hibernates or sleeps, cron does not run. You may have noticed that the files inside *my cron.daily folder, in the image, is green. That is because if the files in there are not executable, they do not run. (Sounds logical, but you would be surprised at how often it bowled someone over).

We can do a quick example by making a script, and putting it in one of the folders, and see if it runs. Just keep in mind that this method does not give one fine-grained control. By this I mean, the file I put in the daily folder will run every day, but I cannot tell it to run at, say, ten to four in the morning. For that we use another method. Open your favourite editor and let’s make a simple script: #!/bin/sh mkdir -p ~/fcm cd ~/fcm touch here.txt Save it, make it executable with chmod +x and we are ready to go. Always test your scripts first. Once you know it works, remove the folder it created and let's pop it into cron.hourly.

Now continue to enjoy your free magazine and you can check it again in an hour. This is an easy way to run scripts, however if you take the LPIC exam, they will want to know if you can remember the minute, hour, day of month, month, day of week. In Ubuntu, it is visible in a helpful comment, but for the exam, they will not give it to you, instead wanting you to remember the order. You can use a mnemonic, like my house of dominoes mowed down, to remember it. If you have trouble visualising how the numbering works, I suggest playing online with crontab guru: https://crontab.guru/ At the bottom of the page, you will see “examples” – I suggest you look closely at those as I recall examples in the test were exactly what you saw there. Let us use this time to do something with crontab (Cron table). Please remember that crontab is different per user. You can specify the user with the tack: -u For now, I want you to do this with your own user or a test user on your system. If your system has a file named /etc/cron.allow (deny is also valid), and you are not listed in there, you will not be able to run crontab. For other users you can do it like this: crontab -u seconduser -e (make sure the user exists, so spell correctly!). This is mainly used when you need to run something as root (be careful! And remember you need sudo).

Please type crontab -l first to see if you have an existing file created. The choice here is up to you, but as it is a newbie tutorial, I’m going with nano. Here in this Image, you can see the helpful “m h dom mon dow command” in the comments. We edit it like shown below. Now every 5 minutes, my script will run. The other way you need to know is using at (where de scripts at? - just kidding). What you need to know for the exam: There is an ‘at daemon’ running in the background, atd, that will check the job queue for jobs to run. The other thing that comes up that you need to know is that the ‘at’ command submits the job to that very queue. Other than that, it did not go into detail. You may have noticed that there is no man entry for ‘at’ in Ubuntu as it is not installed by default. (It is not difficult to install). So, if you want to know more, I’ll be pointing you to the interwebs as I have limited space in the magazine: https://www.geeksforgeeks.org/at-command-in-linux-with-examples/

Because things change in exams, and the last time I gave a class in this, it was two exam versions ago, I’d just like to point out that ‘at’ does not only consist of ‘at’ and its daemon, there are other files involved, namely: /usr/bin/at /usr/bin/atq /usr/bin/atrm /usr/bin/batch /usr/sbin/atd /usr/sbin/atrun Since the LPIC course is distro agnostic, I’d suggest you file these away somewhere that you can recall them if needed. The whole ‘running a command or script at a certain time’ is not hard, but the LPIC exam guide did a poor job of explaining this easy topic, instead making it seem as difficult as possible, explaining from the arse end up. My suggestion is do it, get used to it, and you will not forget it. A favourite of mine for homework, was to get students to play a gong wave file every hour via cron. I suggest you try and do that too, then maybe make it play every hour and one minute and so forth, making a game out of it will make it stick much better than any Youtube video will. As always, if I made a mistake: misc@fullcirclemagazine.org

issue210/c_c.1730095439.txt.gz · Dernière modification : 2024/10/28 07:03 de d52fr