Score:0

Can't get crontab to work as expected

id flag

I want to do a daily backup of my home directory by executing the following script (I can write to /home):

   #!/bin/sh -e
   cd /home
   ls -l 
   rm -f silbar.tar.gz
   echo "starting tar"
   tar -czf silbar.tar.gz silbar
   cd ./silbar
   echo "done with tar"
   beep

To do so I created my personal crontab file by enter ~$ crontab -e and added the following (and only line which is not a comment)

30 6 * * * mybackup.script 

assuming that my crontab would run in my home directory.

But nothing happens. Doing ~$ grep CRON /var/log/syslog shows, apart from root things, the following error:

Aug 30 06:30:01 Puma CRON[100126]: (silbar) CMD (mybackup.script )

Aug 30 06:30:01 Puma CRON[100125]: (CRON) info (No MTA installed, discarding output)

So, what am I doing wrong? What is MTA, anyway?

user535733 avatar
cn flag
Your script has an error. We don't know what the error is. Cron is attempting to e-mail you the error message, but your system isn't set up for that. Instead of mucking about setting up an MTA for cron, simply redirect the error output to a file so you can read it.
user535733 avatar
cn flag
Example: `30 6 * * * /home/silbar/mybackup.script > /home/silbar/backup.log 2>&1`
cn flag
please do not use /home for personal files. The only thing in there should be user directories. Put them elsewhere; also never put more than 1 home in tar file; use 1 file per /home/USER/ and put it in /home/USER (consider it "better practice"). It is a confidentiality breach if you don't. And always use absolutes paths when using cron. It is the safest method. The "rm" is not needed. "tar" by default overwrites without asking.
Score:1
cn flag

You may need to enter the full path of your script in your cronjob.

Cronjobs run with an almost empty environment. Thus, executables may not be found. The same goes for the commands in the script: provide full paths to commands, or set up a PATH variable in your script.

Score:0
it flag

By beginning with #!/bin/sh -e, you cause your script to run with sh (bashis better IMHO), and -e exits on any error (man set), so one cannot handle errors. Your script is failing, and cron is trying to email you the log. cron can't find a Message Transfer Agent (MTA). Email hasn't been configured on your system, even for local delivery.

Jobs run through cron, or systemd startup scripts aren't run in the same runtime environment that you have on your desktop. systemd startup scripts are run as root. None of your PATH changes, or other environment variable settings from ~/.bashrc are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

Score:0
id flag

Wow!, very useful answers from user535733, Rinzwind, Vanadium, and waltinator. I now have it working as suggested, using bash and full paths. The backup now goes to a flash drive (which is where I wanted it to go, anyway) instead of to /home.

Thanks to all, Dick

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.