Posted by6 months ago
Archived
Traditionally, I have written all of my programs that need to run indefinitely by putting it in a
while True
loop with a time.sleep(x)
at the end. An example program would be one that gathers data from a number of APIs, processes the data, and writes some of the processed data to a DB.I have recently learned that you can achieve the same result using a cron job. In my research, I also found apscheduler.
I have a few questions related to this information:
- Which method is best for my scenario listed above? I'd imagine a cron job would be preferable.
- How do I know which method to choose in different programs and different scenarios?
- If the program uses x threads (one for each API) that do not rely on each other, would it be ideal to run x cron jobs with the different variables at the same time rather that relying on multi-threading in the program?
- A program written to specifically be used with a cron job vs a
while true
program would be written pretty differently. If this is an open source program, how do you inform the user? Do you simply put a line in the README? - Is there any other method that I am missing to achieve the same result? Are these the 'standard' way of running programs indefinitely?
Better Than Cron Jobs In California
8 comments
I am looking for a better way to log cronjobs. Most cronjobs tend to spam email or the console, get ignored, or create yet another logfile.
In this case, I have a Nagios NSCA script which sends data to a central Nagios sever. This send_nsca script also prints a single status line to STDOUT, indicating success or failure.
This emails the following message to root@localhost, which is then forwarded to my team of sysadmins. Spam.
forwarded nsca_check_disk: 1 data packet(s) sent to host successfully.
I'm looking for a logging method which: Call of duty bloodthirsty.
- Doesn't spam the messages to email or the console
- Don't create yet another krufty logfile which requires cleanup months or years later.
- Capture the log information somewhere, so it can be viewed later if desired.
- Works on most unixes
- Fits into an existing log infrastructure.
- Uses common syslog conventions like 'facility' and 'priority'
- Can work with third party scripts which don't always do logging internally.
Stefan Lasiewski
![Cron job time Cron job time](/uploads/1/2/3/7/123704541/128440817.png)
15.5k3131 gold badges111111 silver badges171171 bronze badges
3 Answers
In the process of writing this question, I answered myself. So I'll answer myself 'Jeopardy-style'. This expands on the answer provided by Dennis Williamson.
The following will send any Cron output to
/usr/bin/logger
(including stderr, which is converted to stdout using 2>&1
), which will send to syslog, with a 'tag' of nsca_check_disk
. Syslog handles it from there. Since these systems (CentOS and FreeBSD) already have built-in log rotation mechanisms, I don't need to worry about a log like /var/log/mycustom.log
filling up a disk./var/log/messages now has one additional message which says this:
I like /usr/bin/logger , because it works well with an existing syslog configuration and infrastructure, and is included with most Unix distros. Most *nix distributions already do log rotation and do it well.
Community♦
Stefan LasiewskiStefan Lasiewski15.5k3131 gold badges111111 silver badges171171 bronze badges
Jobber Vs Cron
Pipe the output through logger.
Edit: Your update looks like the right way to go.
Dennis WilliamsonDennis Williamson51.6k1212 gold badges9393 silver badges132132 bronze badges
You can also run
crond
with option -s
(or -S
in Busybox) so that output is sent to syslog
.Stuart CardallStuart Cardall