Crontab Job Not Working – Troubleshooting Guide

bashcommand linecronscripts

Backgound

I am trying to run an sh script every minute using crontab, but it is not working.

Problem

When I run the script manually, it executes properly, however crontab can't do it.

I created the job using crontab -e, and I can see cron is running because if I type pgrep cron I get the PID in return.

I also know that my time format is correct because I tested it with this online tester.

Code

wallpaperSlider.sh:

#!/bin/bash
feh --randomize --bg-fill /home/username/Pictures/wallpapers/*

crontab job:

SHELL=/bin/bash

* * * * * username /home/username/.crons/wallpaperSlider.sh

Research

I have read the most common errors in AskUbuntu and I don't think I am experiencing any.

I understand that perhaps I am missing some environment variable, but I am not sure how to check that.

I also know that crontab -e changes/creates a tmp file, in my case /tmp/crontab.wCajAu/crontab.

Question

  1. How can I make this script execute in cron?
  2. Having in mind that crontab -e changes a file in the tmp folder, will I lose all changes once I reboot?

Best Answer

Your cron format is wrong. You want:

* * * * * /home/username/.crons/wallpaperSlider.sh

User's crontabs don't have a username field. That is only used for system-wide crontabs like /etc/crontab. You also don't need SHELL=/bin/bash since, even if your default shell isn't bash (it is dash on Ubuntu), your script itself has the shebang line (#!/bin/bash) so it will be run by bash no matter what shell cron launches.

You will probably have other issues as well though, since you're trying to run an application that communicates with the X server from cron. If so, you need to use:

DISPLAY=":0.0"
XAUTHORITY="/home/YOURUSERNAME/.Xauthority"
XDG_RUNTIME_DIR="/run/user/1000"
* * * * * /home/username/.crons/wallpaperSlider.sh
Related Question