crontab jobs don't execute

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
joefred

crontab jobs don't execute

Post by joefred »

I have been trying to create automated scripts for backing up my desktop system and my /Home/user/Documents.
System:
Linux mintJF 4.15.0-43-generic #46-Ubuntu SMP x86_64 GNU/Linux
Cinnamon Desktop

The scripts work when run from the terminal but not when scheduled with crontab -e
Script 1:
#!/bin/bash
sudo tar -cvzpf $Backup_Destination/$Backup_Snapshot_file_name-$Backup_file_timestamp.tar.gz --exclude=/dev --exclude=/run --exclude=/media --exclude=/home --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/var --exclude=/tmp -g $Snapshots_Destination/$Backup_Snapshot_file_name-$Snapshot_file_timestamp.snap $Data_to_be_Backed_up

Roots crontab job:

0 0 * * * /bin/bash /home/myuser/Full_Backup_Systems_V1.sh

This worked from root crontab but not when placed in my user crontab.

log:
CRON[20176]: pam_unix(cron:session): session opened for user root by (uid=0)
CRON[20177]: (root) CMD (/bin/bash /home/myuser/scripts/Full_Backup_Systems_V1.sh)

Success, I think. But...

Script 2:
#!/bin/bash
sudo tar -cvzpf $Backup_Destination/$Backup_Snapshot_file_name-$Backup_file_timestamp.tar.gz -g $Snapshots_Destination/$Backup_Snapshot_file_name-$Snapshot_file_timestamp.snap $Data_to_be_Backed_up

**I know its the same script but variables are adjusted**

my user crontab jobs:

0 0 * * * /bin/bash /home/myuser/Full_Backup_Systems_V1.sh

5 20 * * 0 /bin/bash /home/myuser/myuser_Documents_Backup.sh

log:
CRON[28641]: (myuser) CMD (/bin/bash /home/myuser/myuser_Documents_Backup.sh)
CRON[28640]: (CRON) info (No MTA installed, discarding output)
CRON[28640]: pam_unix(cron:session): session closed for user myuser

I added MAILTO=""
ran it through Cron again and next log reads:

CRON[30345]: pam_unix(cron:session): session opened for user myuesr by (uid=0)
CRON[30348]: (myuser) CMD (/bin/bash /home/myuser/myuser_Documents_Backup.sh) CRON[30345]: pam_unix(cron:session): session closed for user myuser

Neither executed with myuser crontab. No further explanation.
I also tried to run Script 2 in root crontab and it failed.
There is clearly an error (permissions, env, sh) but nothing I'm finding online seems to work or I'm not understanding.

Please advise
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
WharfRat

Re: crontab jobs don't execute

Post by WharfRat »

Remove /bin/bash so it's just /home/myuser/Full_Backup_Systems_V1.sh

Is the script executable :?:
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: crontab jobs don't execute

Post by rene »

joefred wrote: Sun Feb 17, 2019 9:47 pm The scripts work when run from the terminal but not when scheduled with crontab -e
So I take it you have setup sudo to not require someone/thing entering a password when invoking tar?

In any case please note there's no need to keep guessing at output: simply install an MTA. Please see viewtopic.php?f=213&t=279203&hilit=postfix#p1537513 for that part.
joefred

Re: crontab jobs don't execute

Post by joefred »

I removed /bin/bash from the crontab job

no change. Log shows cron open job then close.

Yes. The scripts are executable.

I added MTA as suggested by rene.

mail message:

/bin/sh: 1: /home/myuser/myuser_Documents_Backup.sh: not found

Does this mean cron cannot find the script?
OR is it looking for sh instead of bash?
What does the 1 indicate?
No clear answer on google search.

Also, I edited the /etc/sudoers.d file to allow myuser to execute tar without a password.

Still doesn't run.
WharfRat

Re: crontab jobs don't execute

Post by WharfRat »

It can't find /home/myuser/myuser_Documents_Backup.sh

Are you sure of the name ls -lh /home/myuser/myuser_Documents_Backup.sh
joefred

Re: crontab jobs don't execute

Post by joefred »

OK. I am an idiot. The path lacked my subdirectory "scripts".
/home/myuser/scripts/myuser_Documents_Backup.sh

Now it is finding the script. However, it will not run. Here is the log message:

CRON[19341]: pam_unix(cron:session): session opened for user myuser by (uid=0)
CRON[19342]: (myuser) CMD (/home/myuser/scripts/myuser_Documents_Backup.sh )
sudo[19346]: pam_unix(sudo:auth): conversation failed
sudo[19346]: pam_unix(sudo:auth): auth could not identify password for [myuser]
CRON[19341]: pam_unix(cron:session): session closed for user myuser

The script does start but seems to hang on the command 'sudo tar'
because this is the message in my mail:

This Script backs up myuser's Documents. [Om

sudo: no tty present and no askpass program specified
?

Well, now I have a new problem. Apparently I messed up the sudoers file and can no longer su ...uh oh.
WharfRat

Re: crontab jobs don't execute

Post by WharfRat »

If you have to have escalated privileges then use root's crontab or /etc/crontab.

It's failing because it can't get your sudo password to allow the escalated privileges.
ajgringo619

Re: crontab jobs don't execute

Post by ajgringo619 »

joefred wrote: Mon Feb 18, 2019 9:13 pm Also, I edited the /etc/sudoers.d file to allow myuser to execute tar without a password.
Still doesn't run.
Did you use visudo to edit the file (you did mean /etc/sudoers, right?)? Can you post - I believe you need to use the full path to the program in question, something like this:

Code: Select all

me 	ALL=(ALL:ALL) NOPASSWD: /usr/bin/timeshift --list
joefred

Re: crontab jobs don't execute

Post by joefred »

Yes. I used this:
sudo visudo
myuser ALL=(ALL) NOPASSWD: /bin/tar

I've changed to using root's crontab. Everything works fine that way.
Locked

Return to “Scripts & Bash”