Try as I might, can't get a script to run in cron

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
I've used cron before, to run jobs.
I know to set my files executable and I know the file works if ran from a shell prompt.
I've tried with and without standard error and standard output enabled.

I've moved the script from a path to a different location where another script used to run fine.
I've edited the script to just 'touch blahlbah.txt' and still, I can not get this script to run.

Script simply contains some rsync jobs.

cd /mnt/SSD1/
rsync -r --delete -v --progress --update /mnt/ARRAY/data/backup /mnt/HDD2-BKUP/ARRAY-DATA/backup
rsync -r --delete -v --progress --update /mnt/ARRAY/data/Scan /mnt/HDD2-BKUP/ARRAY-DATA/Scan
rsync -r --delete -v --progress --update /mnt/ARRAY/data/Photos /mnt/HDD2-BKUP/ARRAY-DATA/Photos
touch test.txt

I even know, that the exact rsync commands will run, as themselves in a cron job, I just don't want to add more cron jobs per path, rather just have 1 cron job perform a batch of copies.
What on earth could I be doing wrong? I tried changing ownership of the script file too.
 

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
I have changed the permission of the cron job itself to my normal account, it's doing a little bit (touching the file) it still refuses to honour the Rsync commands in the script.
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
Please post the contents of at least one of the scripts enclosed on CODE tags.
 

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
Code:
touch /mnt/SSD1/test.txt
touch /mnt/ARRAY/data/backup/test.txt
rsync -r --delete -v --progress --update  /mnt/ARRAY/data/backup   /mnt/HDD2-BKUP/ARRAY-DATA/backup
rsync -r --delete -v --progress --update  /mnt/ARRAY/data/Scan   /mnt/HDD2-BKUP/ARRAY-DATA/Scan
rsync -r --delete -v --progress --update  /mnt/ARRAY/data/Photos   /mnt/HDD2-BKUP/ARRAY-DATA/Photos
rsync -r --delete -v --progress --update  '/mnt/ARRAY/data/Photos_Unsorted'   '/mnt/HDD2-BKUP/ARRAY-DATA/Photos_Unsorted'
rsync -r --delete -v --progress --update  '/mnt/ARRAY/data/!Docs'   '/mnt/HDD2-BKUP/ARRAY-DATA/!Docs'
rsync -r --delete -v --progress --update  '/mnt/ARRAY/data/!Apps & software'   '/mnt/HDD2-BKUP/ARRAY-DATA/!Apps & software'
touch /mnt/ARRAY/data/backup/test2.txt
 

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
Script will run, script will even run the 'touch' commands, it won't perform the rsyncs
If I manually run the command from the shell, it works.
 
Last edited:
Joined
Jun 2, 2019
Messages
591
1. Does the script contain the bash shebang header?
#!/bin/sh
2. Did you add execute permission to the script?
chmod a+x {filename}
3. What does your CRON entry look like?
Screenshot 2022-07-18 at 2.01.16 AM.png

4. You may have to prefix the rsync command with the full path
which rsync
/usr/local/bin/rsync
 

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
1. Does the script contain the bash shebang header?

2. Did you add execute permission to the script?

3. What does your CRON entry look like? View attachment 56932
4. You may have to prefix the rsync command with the full path

"#!/bin/sh"

I don't truly understand this thing but there it is, thank you!
You'd think after fiddling with this stuff 10 years it would stick!
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
/usr/local/bin is not in cron's default search path.
 

Arwen

MVP
Joined
May 17, 2014
Messages
3,611
PATH issues are the number 1 cause of cronjob failures that I have seen. (And I am guilty of it occasionally...)
 

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
I should know better after this long, so what precisely does #!/bin/sh actually tell the system?

"Please be aware, I'm an executable script and you'll need to search other locations to locate the binaries I might call" ?
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
No. #!/bin/sh tells the system "this is an executable script and please feed the contents of the script to the interpreter /bin/sh for execution." The script is written in the "sh" programming language so you need to feed it to that interpreter. You can start scripts with #!/usr/local/bin/perl or #!/usr/local/bin/python if they are written in the Perl or Python programming languages, respectively. I'm simplifying a little bit (for the pro's: let's skip "env" here for simplicity), but that's essentially "it".

The search path for executables is part of a process' environment. And it is documented behavior that cron runs with a path set to /usr/bin:/bin - which are the directories where system binaries are kept. /usr/local/bin is not part of FreeBSD proper but the place where additional 3rd party software lives. So you need to write that into your shell scripts explicitly or configure a path like /usr/local/bin:/usr/bin:/bin in your crontab. Unfortunately the latter method is not available in the TrueNAS UI, although it is in plain FreeBSD on the command line.

HTH,
Patrick
 

diskdiddler

Wizard
Joined
Jul 9, 2014
Messages
2,377
Ok this makes a reasonable amount of sense to me for a change, so I get most of that.

Thank you - I've made a few scripts in my time but I often just copy other peoples and edit, hence the lack of that header in the file.
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
Does the script contain the bash shebang header?
Nitpick: /bin/sh is not bash on FreeBSD. And not even on Debian or Ubuntu. One should never assume bash writing shell scripts but stick to POSIX.
 
Top