Running a script with crontab

Hello, I have a script for backing up my files to a external drive and I want to do this every day at a certain time, I was using crontab as a standard user and it was working fine, I decided change my script so that I can mount and unmount the drive before and after running it, this required me to run the script as root, so I tried to seet up a crontab as root using crontab -e but it never runs and when I try: systemctl start cron the terminal outputs: Failed to start cron.service: Unit cron.service not found why is this and how do I fix it?

cron is cron.
systemd services are systemd services.
They are not the same thing.
But … in most cases you would want to use the more modern and robust systemd for things you would have previously done using cron.

To the specifics … maybe you arent finding a ‘cron’ service because its ‘cronie’, at least on my plasma.

There are many cron implementations, but none of them are installed by default as the base system uses systemd/Timers instead. See the Gentoo’s cron guide, which offers comparisons.

After installation, the daemon will not be enabled by default. The installed package likely provides a service, which can be controlled by systemctl. For example, cronie uses cronie.service .

Check /etc/cron.daily/ and similar directories to see which jobs are present. Activating cron service will trigger all of them.

Note: cronie provides the 0anacron hourly job, which allows for delayed runs of other jobs e.g. if the computer was switched off at the moment of standard execution.

Here is a link directly to overview of cronie, but you might find the whole page interesting:

@cscs okay, so how would I setup a systemd timer to run my script every day at 1 pm?

(though again … something like cronie can of course be used too if thats more comfortable for you)

Yes cron looks easier, how would i install/ use cronie?

first … check it isnt installed or running already, ex:

pacman -Qs cron
systemctl status cronie

but go back and refer to the link I posted … everything is there.

okay so i installed it, what would I put inside the file i create in /etc/cron.d besides 0 13 * * * /home/user/ ?

Nothing really… cronie supports the tradition crontab too…

To quote the page again:

Cronie provides both cron and anacron functionalities: cron runs jobs at regular time intervals (down to a granularity of one minute) as long as the system is available at the specified time, while anacron executes commands at intervals specified in days. Unlike cron, it does not assume that the system is running continuously. Whenever the system is up, anacron checks if there are any jobs that should have been run and handles them accordingly.

A cron job can be defined in a crontab-like file in the /etc/cron.d directory or added within the /etc/crontab file. Note the latter is not present by default but is used if it exists. As instructed by /etc/cron.d/0hourly , any executable file in /etc/cron.hourly will be run every hour (by default at minute 1 of the hour). Files in /etc/cron.minutely are executed every minute if instructed accordingly in /etc/cron.d/0hourly . The executables are typically shell scripts, symlinks to executable files can also be used. The /etc/cron.deny file includes the list of users not allowed to use crontab, without this file, only users listed in /etc/cron.allow can use it.

Anacron works similarly, by executing the files in the /etc/cron.daily , /etc/cron.weekly and /etc/cron.monthly directories, placed there depending on the desired job frequency. The cron job /etc/cron.hourly/0anacron makes sure that anacron is run once daily to perform its pending tasks.

And they provide an example (technically to show how to quiet the ‘mailto’ function):

0 1 5 10 * /path/to/ >/dev/null 2>&1

so i can just type crontab -e ?

so, i did crontab -e and added 40 16 * * * /home/user/ > /home/user/log.txt but the log file was created but it was empty and the script did not run.

Is the script executable? Does it work running it manually?
What does the log say?
Does the service/systemd provide any errors?

Okay, so it runs manually but i get this is what systemctl status cronie shows:

Dec 27 16:41:01 noah-82dq CROND[20934]: (root) CMDOUT (/bin/sh: /home/noah/ Permission denied)
Dec 27 16:41:01 noah-82dq CROND[20934]: pam_unix(crond:session): session closed for user root
Dec 27 16:45:01 noah-82dq crond[959]: (root) CAN'T OPEN (/etc/crontab): No such file or directory
Dec 27 16:45:01 noah-82dq crond[959]: (CRON) bad command (/etc/cron.d/backup)
Dec 27 16:46:01 noah-82dq crond[959]: (root) CAN'T OPEN (/etc/crontab): No such file or directory
Dec 27 16:48:01 noah-82dq crond[959]: (root) CAN'T OPEN (/etc/crontab): No such file or directory
Dec 27 16:48:01 noah-82dq crond[959]: (root) RELOAD (/var/spool/cron/root)
Dec 27 16:49:01 noah-82dq crond[21198]: pam_unix(crond:session): session opened for user root(uid=0) by (uid=0)
Dec 27 16:49:01 noah-82dq CROND[21199]: (root) CMD (/home/noah/ >/dev/null 2>&1)
Dec 27 16:49:01 noah-82dq CROND[21198]: pam_unix(crond:session): session closed for user root

So thats the problem.
If you are running this script as root, I suggest you place and privilege it with root things.
(not in HOME)
And if it should just be user home stuff … you should be using User services.

Also … maybe you are missing this important part:

The /etc/cron.deny file includes the list of users not allowed to use crontab, without this file, only users listed in /etc/cron.allow can use it.

so i should put the script in the / directory?
because I need to run the script as root to mount the drive

I might put it somewhere like /opt/ or /usr/local/bin

Oh yeah and one more thing:

Cronie is particular about the permissions for /etc/cron.d/0hourly. None of the tasks in /etc/cron.d/{hourly,weekly,daily} … etc will be run (including the anacron launcher) if /etc/cron.d/0hourly is damaged or has improper permissions. pacman -Qkk cronie can show if you have any such issues.

It works now! Thanks for taking your time to help!

Cheers. Glad its working :slight_smile:

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.