3

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?
2
  • I am following this tutorial: cyberciti.biz/faq/… – Flame_Phoenix Jul 28 '17 at 14:22
  • If some envvars missing it is important which desktop environment (DE) you using. In this answer I did a script that does a similar job. – pa4080 Jul 28 '17 at 14:33
3

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
4
  • I have updated my question with additional info, feel free to let me know your thoughts ! – Flame_Phoenix Jul 28 '17 at 14:30
  • @Flame_Phoenix did you try the solution I gave in my answer? And that file in tmp is just a tmp file, it will be renamed and saved when you save and close the editor window that is opened by crontab -e. – terdon Jul 28 '17 at 14:31
  • I just did ! It worked! I somehow have the feeling I ended up choosing the least intuitive possible solution ... how was I supposed to know I needed those specific variables? And that I needed all of them? Thanks for the help! – Flame_Phoenix Jul 28 '17 at 14:33
  • @Flame_Phoenix you weren't :) Cron wasn't designed to run GUI stuff, so making it work with GUI is a bit complicated. Those are the main variables needed to let a process connect to your running X server. – terdon Jul 28 '17 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.