cron won't run shell script that works in terminal

i created the following script to create a tar archive of a directory and it's contents. the rm command is removing a previous archive that exists in the folder before executing tar to create a new one:


#!/bin/sh
# Script to fetch iOS backup for iphone and tar it in the archives folder
export PATH=$PATH:/usr/bin
rm -rv /Users/username/Documents/iOS\ Archives/username\ iphone\ archive.tar
tar -cvf /Users/username/Documents/iOS\ Archives/username\ iphone\ archive.tar /Users/username/Library/Application\ Support/MobileSync/Backup/00008110-000174E83682801E/*


i made it executable by running:


chmod +x scriptname.sh


and i was able to execute it successfully from terminal by typing:


./scriptname.sh



but i can't figure out how to get cron to run the script. i added the following line:


00 19 * * 1 /Users//Documents/Scripts/\ iphone\ archive.sh


nothing happens at the event day and time


does anyone have any guidance ? i am not a programmer or IT person, but want to do this in order to get away from using apple's archive utility for creating my archives.


[Edited by Moderator]

MacBook Pro 16″, macOS 11.6

Posted on Jan 31, 2022 4:15 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 4, 2022 9:41 AM

i apologize, the code i put into the post on here was edited and i mistyped or accidentally deleted that portion of the line for name of script, i was editing out references to my real name. it was actually correct in the shell script itself.


ultimately, i ended up with this script using the script filename "username iphone archive.sh" :


#!/bin/zsh
export PATH=$PATH:/usr/bin
rm -rv /Users/username/Documents/iOS\ Archives/username\ iphone\ archive.tar
sleep 1s
tar -cf /Users/username/Documents/iOS\ Archives/username\ iphone\ archive.tar /Users/username/Library/Application\ Support/MobileSync/Backup/00008110-000174E83682801E


and this works on it's own in a terminal when i type:


./username\ iphone\ archive.sh


ultimately, what i had to do in order for it to run without input when called by crontab is to add cron to the Full Disk Access section.


i know that crontab is ancient, but for my needs if it still does the job i would use it for now. if there's some easy way to convert crontab jobs over to launchd without learning scripting needed to modify launchd the right way, i'd rather avoid it since i don't have much time to learn it now.

13 replies
Question marked as Top-ranking reply

Feb 4, 2022 9:41 AM in response to Camelot

i apologize, the code i put into the post on here was edited and i mistyped or accidentally deleted that portion of the line for name of script, i was editing out references to my real name. it was actually correct in the shell script itself.


ultimately, i ended up with this script using the script filename "username iphone archive.sh" :


#!/bin/zsh
export PATH=$PATH:/usr/bin
rm -rv /Users/username/Documents/iOS\ Archives/username\ iphone\ archive.tar
sleep 1s
tar -cf /Users/username/Documents/iOS\ Archives/username\ iphone\ archive.tar /Users/username/Library/Application\ Support/MobileSync/Backup/00008110-000174E83682801E


and this works on it's own in a terminal when i type:


./username\ iphone\ archive.sh


ultimately, what i had to do in order for it to run without input when called by crontab is to add cron to the Full Disk Access section.


i know that crontab is ancient, but for my needs if it still does the job i would use it for now. if there's some easy way to convert crontab jobs over to launchd without learning scripting needed to modify launchd the right way, i'd rather avoid it since i don't have much time to learn it now.

Feb 1, 2022 9:38 AM in response to zero7404

Well, from your post, the most obvious problem is that you:


a) named your script scriptname.sh


but b) setup cron to run a completely different command:


00 19 * * 1 /Users//Documents/Scripts/\ iphone\ archive.sh


' iphone archive.sh' is not the same as 'scriptname.sh'


Just in case you obfuscated one, but not the other name, does the script name really begin with a space?

Also, did you remove the username from the posted path? because '/Users//Documents...' is not going to be a valid path.


Ultimately, though, crontab functionality has largely been moved into launchd which has a different control mechanism. It may be worth moving to that instead.

Feb 4, 2022 10:28 AM in response to zero7404

Colons are path separators.


This is the default environment inside of a cron job

PWD:    /Users/me

id -a:  uid=501(me)
        gid=501(me)
        groups=501(me),
               204(_developer),
               100(_lpoperator),
                98(_lpadmin),
                80(admin),
                61(localaccounts),
                12(everyone)

$#:     0  # no command line arguments.

printenv:
        SHELL=/bin/sh
        USER=me
        PATH=/usr/bin:/bin
        PWD=/Users/me
        SHLVL=1
        HOME=/Users/me
        LOGNAME=me
        _=/usr/bin/printenv


Feb 4, 2022 10:43 AM in response to zero7404

If your cron job is working, then just move on. Extra stuff in PATH is not harmful if it is not used, and if it is needed to find the commands you use, then it is beneficial.


The point I'm trying to make is that a cron environment is not the same as an interactive login, where you run shell initialization scripts.


And unless you explicitly invoke another shell, you get /bin/sh which does not have all the magic that bash or zsh has.

Feb 4, 2022 3:23 PM in response to zero7404

#!/bin/zsh


as the first line of a script will use zsh as the shell.


BUT the script must be executable

chmod +x /path/to/script


I would strongly suggest you not have any spaces in your script path, including the script name. Unix is very unfriendly to file names with spaces. Unix can deal with spaces in file names, but at a cost to you in time and frustration.


I have been working with Unix since 1985 and Macs since 1988, and macOS files and folders with spaces in them still trip me up. And I am not a casual user. I spend my days working in Terminal sessions ssh’ed into Unix systems from my Mac writing C source code, shell scripts, awk scripts, perl scripts, etc… during my work day. I am very familiar with getting caught by macOS files with spaces.

Feb 4, 2022 9:47 AM in response to BobHarris

i personally was not sure how the PATH line works and what should be specified.


here's the path variable when i lookup tar:


/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public tar


i don't know what the colons are meant to represent, separators ?

and when i lookup the path for cron, I get the same output


so in my script i chose the first one on the left.

Feb 4, 2022 10:38 AM in response to BobHarris

should i revise the PATH variable that i have in my script to this one ?


/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public tar


the tar job seems to execute fine although i don't see any warning messages when the job is running because it is not run in an open terminal session.

Feb 5, 2022 3:07 PM in response to BobHarris

thanks for the tip on this. although the jobs seem to be running fine with the executable that has spaces in it's file name, i'll keep the advice in mind.


i'm a novice with scripting and terminal use but do my research completely for what i want to achieve before doing it. i typically avoid special characters in general wrt file names, even when in the confines of a GUI (windows/mac/ubuntu), except the SPACE and underscore "_" and period "." characters are those i commonly use in filenames. the rest I avoid even though I know many are supported.


at the moment i seem to be getting tripped on writing an automatic smb share mount correctly .... wondering if you could help me with that ? here's the thread :


https://discussions.apple.com/thread/253637636


any assistance would be greatly appreciated

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

cron won't run shell script that works in terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.