Thursday, April 25, 2024
HomePython6 Ideas Earlier than You Write Your Subsequent Bash Cronjob

6 Ideas Earlier than You Write Your Subsequent Bash Cronjob


Hello pretty individuals! 👋 As a part of a analysis I’m doing I needed to write some bash scripts which had been alleged 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 if you write your individual bash scripts as cronjobs.

So with out losing any time lets start!

I’ve heard individuals say since endlessly 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 kinds of bizarre errors. At first, I believed my laptop has gone rogue however fortunately the issue was easier. I had forgotten to write down this one line on the prime of my script:

#! /bin/bash

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
  • */15 makes the command run each quarter-hour
  • >> is redirecting the output to the pipe.log file
  • 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 probably the most embarrassing challenge. I used to be operating a Whereas true loop in my bash script. When you find yourself 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 saved on operating a brand new occasion each quarter-hour however the previous 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 .....

performed

This makes positive 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 could possibly till it receives a stop or terminate sign. For my cronjob I wished it to run for less than 4 minutes and after that self-terminate.

The answer was to make use of the timeout command:

timeout 240 tcpdump <args_for_tcpdump>

240 implies that tcpdump will robotically stop after 240 seconds. This may 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 traditional consumer and one is for root. In case your script requires sudo privileges then it’s essential to put your job in root’s crontab.

Whenever you sort crontab -e within the terminal you’re enhancing present consumer’s crontab. Nevertheless, if you sort sudo crontab -e you’re 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 through 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 known 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 standard 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 technique as a result of I knew I wasn’t going to make use of this script on some other machine so I might 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 glad as a clam 😄 See you all within the subsequent submit the place we’ll end up our greenhouse monitoring system! ❤️


References

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments