Author Topic: How to create a cron job in Manjaro  (Read 1487 times)

0 Members and 1 Guest are viewing this topic.

Offline Heart Of A Lion

  • Sr. Mitglied
  • ****
  • Posts: 313
  • I'm new. Be nice!
  • Branch: Stable
  • Desktop: Xfce, Enlightenment
  • GPU Card: ATi 7770 . . . . . . . CPU: AMD octacore 4GHz
  • GPU driver: Free (video-ATI)
  • Kernel: 4.6 (x64)
  • Skill: Intermediate
How to create a cron job in Manjaro
« on: 09. May 2016, 21:53:05 »
In this tutorial you will learn how to create a cron job in Manjaro using Cronie. Cronie is an implementation of cron and it's what Manjaro uses to handle cron jobs. Cron jobs are automated scheduled tasks. They can be used to run/execute a single command or a script or even an entire directory full of scripts. All done automatically and scheduled when you want it to be executed.

These are the steps you need to take to create a cron job in Manjaro:

-----------------------------------------------------------------------------


1. In the /etc directory create a new directory called cron.minutely . This will be the directory where your scripts are placed that need to be run every minute. It's in addition to the directories called /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly which already exist. (You might need to run your file manager as root to be able to create the new directory)

2. In the directory cron.d create a new file without an extension called mycronjobs. (You might need to run your file manager as root to be able to create it)

3. In your mycronjobs file, paste the following code:
Code: [Select]
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

*/1 * * * * root run-parts /etc/cron.minutely #Runs a cron job script every minute
01 * * * * root run-parts /etc/cron.hourly #Runs a cron job script every hour
02 4 * * * root run-parts /etc/cron.daily #Runs a cron job script every day
22 4 * * 0 root run-parts /etc/cron.weekly #Runs a cron job script every week
42 4 1 * * root run-parts /etc/cron.monthly #Runs a cron job script every month

4. Now in your /etc/cron.minutely directory place the scripts you want to run every minute. Note that these scripts should not have an extension at all, like for example .sh, as certain characters are not allowed in the filename of scripts that are run as cron jobs.

5. If there are issues and your script isn't executed correctly, then run the following command in a terminal and hit the 'end' key on your keyboard to analyze the latest log messages:
Code: [Select]
journalctl -xb -u cronie
-----------------------------------------------------------------------------


Congratulations, you have now reached the end of the tutorial.

For more advanced uses of cron jobs take a look at the (optional) notes below.



Optional notes for advanced uses:

*Note 1: If there is no HOME variable in your cron script (which is the case in this tutorial), then the filesystem's root directory will be set to /root/ for cron jobs. If a HOME variable is set in the cron file to HOME=/, then the filesystem's root directory will be set to / for cron jobs. This is a subtle difference that needs to be kept in mind in case you set the HOME variable, but couldn't find the output of your scripts (like written files for example).

*Note 2: If you want to launch graphical program you have to export the display in your script, which means you have to add the following line at the end of your script:
Code: [Select]
export DISPLAY=:0
However just exporting display is not enough. You need to authorize root to use <user>'s display. See this link for more information: https://www.x.org/archive/X11R6.8.0/doc/xhost.1.html
Code: [Select]
xhost + [something]
Then you can launch a graphical program via your script in the following way:
Code: [Select]
dbus-launch <command>
*Note 3: To run a script every 5 minutes, you can use the following command:
Code: [Select]
*/5 * * * * root run-parts /etc/cron.minutely #Runs a cron job script every 5 minutes

Credits:
A thank you goes out to the following Manjaro forum members whose efforts have contributed to making this tutorial possible:
badbodh, excalibur1234, scachemaille
« Last Edit: 10. May 2016, 17:34:20 by Heart Of A Lion »

Offline scachemaille

  • Held Mitglied
  • *****
  • Posts: 528
  • I'm new. Be nice!
  • Branch: Unstable
  • Desktop: Plasma 5
  • GPU Card: nVidia GT 330M
  • GPU driver: Nouveau
  • Kernel: 4.8_x64
  • Skill: Intermediate
Re: How to create a cron job in Manjaro
« Reply #1 on: 09. May 2016, 21:57:25 »
it's a good start.
but by default the scipts  in all pre-existant /etc/cron.xxx folder are already executed by cron/anacron. wihout the need to add the lines you add in your mycronjobs file.