Help with running cronie from /usr/local/bin as root

Hey guys,

I’d need help with a cronie job that isn’t firing.

I’ve been using the above conversation as reference and 2 of my cronie jobs are firing well. The third one, sadly, doesn’t yet.

Here’s my /etc/crontab file:

# /etc/crontab: configuration file for cron

# See cron(8) and crontab(5) for details.

*/5 * * * * donatus /usr/local/bin/CEFCOM
*/5 * * * * donatus /usr/local/bin/CEFCON
* */10 * * * root /usr/local/bin/CachingMergerfs

The 1st 2 jobs run perfectly fine, the third one doesn’t. And it doesn’t matter if i declare the user as “donatus” or as “root”.

systemctl status cronie doesn’t print anything either.

Due to the nature of the script it should be firing, it needs to run as root. How can I achieve that with crontab though?

Also, the script itself works perfectly fine if I call it from a terminal or elsewhere.

Any help will be greatly appreciated :pray:

Do you not have cronie installed ?
Even then it should print something like

Unit cronie.service could not be found.
1 Like

I’m very sorry for being super imprecise. It was late when I posted this.

It does print things, it just doesn’t print anything about /usr/local/bin/CachingMergerfs was what I wanted to say. Sorry

Crontab.guru explains it as

“At every minute past every 10th hour.”

next at 2025-01-06 10:57:00
then at 2025-01-06 10:58:00
then at 2025-01-06 10:59:00
then at 2025-01-06 20:00:00
then at 2025-01-06 20:01:00

Are you sure you mean this, and as you tested it, was it in a 10th hour?

1 Like

Oh! That’s a good catch in general! I misread the info from Crontab.guru and fixed the values, thank you!

Maybe it just wasn’t fired yet because the schedule wasn’t set properly

How frequently do you plan to run that job? Because, according to Crontab.guru - The cron schedule expression generator, you plan to run the job “At every minute past every 10th hour.”

So, that would be 10:00:00, 10:01:00, 10:02:00 etc, then 20:00:00, 20:01:00, 20:02:00 etc, then 00:00:00, 00:01:00, 00:02:00 etc, then 10:00:00 etc.

Also, instead of putting everything in the etc/crontab file, you might be better off creating an individual file for each job in the /etc/cron.d drop-in directory. That might give you more options for each job regarding the shell used etc compared to using the /etc/crontab file.

For example, here is the /etc/cron.d/0hourly file:

# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

Edit: and I see that @xabbu has also noticed the unusual scheduling of the job while I was typing this.

1 Like

Thank you very much for that hint!

As the scripts are laying inside /usr/local/bin would this work like that? And they’d be triggered every hour at :00, correct?

# Run the hourly jobs
SHELL=/bin/bash
PATH=/usr/local/bin
01 * * * * root run-parts /etc/cron.hourly

No.

No.

Maybe a better example

# Run the hourly jobs
SHELL=/bin/bash
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# MAILTO=root
0 * * * * root CachingMergerfs

The PATH is just so you don’t need to use a absolut path for your script/program. If you want every hour at :00 use 0.
Of course you can do

# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# MAILTO=root
0 * * * * root /usr/local/bin/CachingMergerfs

If your Path is just PATH=/usr/local/bin and CachingMergerfs is a Shell script, you would need to use an absolut path for all programs that are not in /usr/local/bin and are called in this script.

Drop in files in /etc/cron.d/ are just another way if you want a cleaner system crontab. And you can set other environment variables, but it is often not needed.

2 Likes

Thank you so much for the detailed explanation!

I think I could follow :joy:

So basically this should work, right?

# Run the hourly jobs
SHELL=/bin/bash
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# MAILTO=root
*/15 * * * * root CachingMergerfs
*/15 * * * * donatus CEFCON
*/15 * * * * donatus CEFCOM

The schedules are just set shorter to see if they’re getting triggered, will adjust once it’s working.

So CEFCON and CEFCOM are working, but sadly CachingMergerfs doesn’t :frowning:

The author of the script says the cron job needs to be run as root, but I’m wondering if it might work if I set the user to donatus as well?

The script works fine if I do it from a terminal, i.e. just type CachingMergerfs.

Is there a way that I can verify that it’s executing the script? Because until now I’m just looking at the System Monitor → Processes in the GUI if the program that the script shall execute is running (which doesn’t).

And systemctl status cronie said:

Jan 06 12:00:00 Morpheus CROND[27268]: (root) CMD (CachingMergerfs)

EDIT: It’s working! If I set user to donatus it works :partying_face:

You can try it, depends on what the script is doing. Is it a Bash script?

As root, as your normal user or with sudo?

Does the script needs environment variables or configuration to run correctly?

Yes, use the journal. For example

sudo journalctl -f 

There will be a notice if cronie starts the script. The sudo might not be required. It might prints a lot, but you know the time when to look at.

You can also do

sudo journalctl -b

The output is in a pager like less, use the arrow keys to find the correct time and look for messages.

You can also do a

sudo journalctl -b -g CachingMergerfs

maybe you see at least a start messages. The -b means only messages form the current boot.

This indicates it was started and ran as user root.

1 Like

It is yes. And it seems to work as normal user with cron.d :+1:

Executing it from terminal works without sudo

No, I don’t think so. Would it help if I provide the script itself? (I’m still learning a lot, not very familiar with scripting/cron)

I can now also see (If run as donatus), that the program/tool the script is supposed to execute is indeed running :slight_smile: So I think it actually works now! Also systemctl status CachingMergerfs is now printing the script itself (It basically fires an rsync job and is shown now in the status of cron) and rsync is running as well :smiling_face:

No, it works now.

Not sure why it was not working in the beginning, maybe it was just a combination of the cron schedule and looking in the wrong place for messages form the program.

1 Like

Thank you so much for helping out! :heart: Made my day :smiling_face: