Ubuntu 18.04.1

I have a series of scripts, written to back up files from locations on my PC to locations on my network storage.

None of the commands appear to require sudo (I can run them all as a user).

All of the locations & files are accessible without root.

All of the scripts are stored in ~/.local/bin

I have a daily cron job set up to run these scripts in sequence. When the script is run by cron rather than by me manually, I have found that elements of the script don't happen.



# use "if test -e" to detect whether a file on the networked drive is online:

if test -e /network-drive/online-test

# if the file is detected, run the backup job. otherwise display an error:


  echo "network drive is online - proceeding with backup" # useful if running from command line

# backup job syntax:

rsync /pc-location/ /network-drive-location/  # rsync command also writes to a log file


# error message:

  notify-send --expire-time=6000 'network drive is not online' 'BACKUP FAILED' # displays a pop up error message

  aplay -q /usr/share/sounds/sound-icons/cembalo-12.wav # plays a sound

  echo "network drive is not online - BACKUP FAILED" # useful if run from command line

  echo "network drive was not online - BACKUP FAILED" > /folder/on/pc/dated-logfile  

# set error exit status so subsequent jobs don't run if && is used:
  exit 1

I have about 7 of these scripts (let's call them script1, script2 etc) backing up from and to different locations on the PC and network drive.

They run fine if triggered from the command line. In tests, if the network drive is offline, the pop-up error message appears, the sound is played, and the log file is written telling me the drive was offline.

I have a cronjob (added with crontab -e) written as follows:

00 16 * * * /home/user/.local/bin/script-1 && /home/user/.local/bin/script-2 && /home/user/.local/bin/script-3 && /home/user/.local/bin/script-4 && /home/user/.local/bin/script-5 && /home/user/.local/bin/script-6 && /home/user/.local/bin/script-7

So at 16:00 daily, cron runs script1 if the PC is on.

If the drive is online, fine, everything works and it moves on through scripts 2-7. If one works, they all work.

If the drive is offline, then the else commands in script1 should run - an error message, a sound, a log file, and then the job should end because of the exit status, and no attempt made to run scripts 2-7.

However I've noticed that one or more of the commands in the else part of the script don't happen.

Most notably - there is no pop-up error message from the notify-send command when the script is run by cron, if the network drive is offline.

Sometimes there is no sound played.

There is always a log file written.

Possible reasons for this inconsistency?

  • 1
    Classic cron new-user mistake. cron is designed to run headless. It does not know about your open windows or login session, so it cannot reliably tell notify-send or aplay where to send pop-ups and sounds. Search how to export your login session into cron-triggered scripts. – user535733 Oct 9 '18 at 0:06
  • Thanks. I did some searching based on your answer and copied the following into the script in the line above the notify-send command: eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)". I don't fully understand what's happening there, but the script now fully works when run by cron, so I'm happy. Thanks for your response. – 4n3wv Oct 9 '18 at 9:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.