Debian and Cron

asl3 3.8-1.deb

Not logging in as root is a PITA. Now we have to be concerned with file ownership and permissions, or nothing works. On the bright side, I've been forced to learn much more about Linux.

I found that scripts that seem to work fine from the command line, do NOT work when run from CRON. Luckily, there is lots of information online to study. Seems that CRON runs as root, which creates problems with those pesky ownership and permission factors.

I found that the best thing to do is preceed the cron entry with ' sudo ', the same as you would do from the command line: * * * * * sudo /etc/asterisk/scripts/myyscript.sh >/dev/null 2>&1.
( if anybody wonders what 2>&1 actually mean here is a short explanation:
2 - refers to a file descriptor used to identify stderr
1 - refers to a file descriptor used to identify stdout
So 2>&1 basically translates to: Redirect stderrtostdout )

I've also had to use ' sudo ' to get SkywarnPlus to run from cron:

Run SkywarnPlus every 10 minutes to check weather alerts

*/10 * * * * sudo /usr/local/bin/SkywarnPlus/SkywarnPlus.py

Also, for debugging, create a log output: ' >tmp/test.log '. This will create a text file of any error encountered by cron.

When in doubt, look it up. There is plenty of answers out there!

Well, that depends. If you are logged in as a normal (non-root) login and exec the crontab -e command then you will be setting up commands to run as your own login. If you use sudo crontab -e then you will be setting up commands that will run as "root".

I wouldn't expect this to be the case IF you are using the "root" crontab.

Most of the issues I have observed is when you have a script that is not running as "root" wants to access a directory/file and doesn't have sufficient privs. Once the issue(s) are understood you can carefully make any needed adjustments that give you the desired functionality while maintaining the security of your system.

What is the difference between user crontab and system crontab?

User crontabs are stored in the /var/spool/cron/crontabs/ directory, with separate files for each user. System-wide crontabs, on the other hand, are typically stored in the /etc/crontab file or in individual files within the /etc/cron. d/ directory.Aug 5, 2023

The easiest way to do the right thing is to stop using user cronabs altogether and use the /etc/cron.d structure. Any file in the /etc/cron.d/ structure will be evaluated as a system-level crontab. This means that it includes the user for which to execute the job.

For example, if you want to have a set of ASL-related things at various intervals, you can simply do the following:

  1. Create a file (for example) /etc/cron.d/asl

  2. Populate the file, one per line, using the following format:

    ${MIN} ${HOUR} ${DOM} ${MON} ${DOW} ${USER} ${COMMAND}
    

    As an example:

    0 * * * * asterisk /usr/local/bin/saysomething
    30 8-20 * * * asterisk /usr/local/bin/saysomethingelse
    17 3 * * 0 root /usr/local/bin/do_something_needing_root
    

    In this example, there will be a) "saysomething" at the top of every hour all day every day, b) "saysomethingelse" between 8am and 8pm at 30 after the hour every day, and c) "do_something_needing_root" at 3:17am on Sundays.

  3. Enjoy cron happiness

Cron is exceedingly well documented online.