Hello pretty folks! 👋 As a part of a analysis I’m doing I needed to write some bash scripts which had been speculated to run each couple of minutes. I made some embarrassing errors alongside the way in which. I’ll write about these errors in hopes that you simply don’t make them if and while you write your individual bash scripts as cronjobs.
So with out losing any time lets start!
I’ve heard folks say since eternally that add a shebang on the prime of your scripts. This tells the terminal which program to make use of to run your scripts in case your script is an executable file. I wrote code in bash however once I requested the terminal to run it, it threw all types of bizarre errors. At first, I believed my laptop has gone rogue however fortunately the issue was easier. I had forgotten to put in writing this one line on the prime of my script:
It’s helpful and in some circumstances even required to learn the output generated by the background job. You may redirect the output of the script to a log file like this:
*/15 * * * * /dwelling/localadmin/Desktop/tcp_script.sh >> /dwelling/localadmin/Desktop/pipe.log 2>&1
*/15makes the command run each quarter-hour
>>is redirecting the output to the
- 2>&1 is ensuring that stderr additionally will get redirected to the identical vacation spot (file descriptor) the place the usual output (
&1) is being redirected
Oh my God, this one was essentially the most embarrassing subject. I used to be operating a
Whereas true loop in my bash script. If you end up drained issues like this may typically creep in. I wished some code to run for some time (4 minutes). The issue was that this infinite loop was additionally a cronjob so cron stored on operating a brand new occasion each quarter-hour however the outdated one by no means stopped executing.
For this explicit downside the answer was to make use of this:
#! /bin/bash finish=$((SECONDS+240)) whereas [ $SECONDS -lt $end ]; do # Different code ..... accomplished
This makes certain that your loop solely runs for 240 seconds. The
SECONDS variable retains monitor of seconds elapsed because the script began executing.
I used to be utilizing
tcpdump to seize wi-fi packets and retailer some details about them. The issue was that
tcpdump retains on operating for so long as it will probably till it receives a stop or terminate sign. For my cronjob I wished it to run for under 4 minutes and after that self-terminate.
The answer was to make use of the
timeout 240 tcpdump <args_for_tcpdump>
240 signifies that
tcpdump will mechanically stop after 240 seconds. This could turn out to be useful if you’re working with one thing like
tcpdump which you wish to terminate when operating from inside a script.
There are two crontabs. One is for the conventional consumer and one is for root. In case your script requires sudo privileges then you must put your job in root’s crontab.
If you sort
crontab -e within the terminal you might be enhancing present consumer’s crontab. Nonetheless, while you sort
sudo crontab -e you might be enhancing root’s crontab. root’s crontab jobs are run with sudo privileges.
Holy moly this one had me pulling my hair out. I used to be operating some jobs by way of root’s crontab. The issue was that my bash script contained program names solely. For instance, I used to be utilizing
ifconfig and I solely had this:
ifconfig mon0 up
However I used to be getting errors that there isn’t any command referred to as
ifconfig. Because it seems root’s crontab doesn’t get entry to the whole set of setting variables and doesn’t find out about all of the instructions that are sometimes accessible in a traditional terminal session.
Just remember to both propagate your setting to root’s crontab earlier than the script is run otherwise you prefix each command with the complete path to that program on disk. I used the latter methodology as a result of I knew I wasn’t going to make use of this script on some other machine so I may get away with hardcoding the paths.
For instance, I rewrote the ifconfig command like this:
/sbin/ifconfig mon0 up
Issues began working and I used to be as completely satisfied as a clam 😄 See you all within the subsequent submit the place we are going to end up our greenhouse monitoring system! ❤️