Ask Different (Apple)
Q&A for power users of Apple hardware and software
Latest Questions
8
votes
3
answers
10489
views
How create a crontab on mac?
I've tried [`crontab -e`](https://unix.stackexchange.com/a/212705/302720) which takes me to what appears to be an empty file opened with vim. I make no changes and simply press esc , : , w , then q to save and exit. But when I try `crontab -l` I see: ``` crontab -l crontab: no crontab for st ``` I t...
I've tried [
crontab -e
](https://unix.stackexchange.com/a/212705/302720) which takes me to what appears to be an empty file opened with vim. I make no changes and simply press esc, :, w, then q to save and exit.
But when I try crontab -l
I see:
crontab -l
crontab: no crontab for st
I tried the same thing [as root](https://superuser.com/a/540693/928461) but I get the same outcome
MacBook-Pro:~ root# crontab -u st -e
crontab: no crontab for st - using an empty one
-- I save an exit vim as above --
crontab: no changes made to crontab
MacBook-Pro:~ root# crontab -u st -l
crontab: no crontab for st
stevec
(5633 rep)
Nov 2, 2020, 10:07 PM
• Last activity: Jul 3, 2025, 04:41 AM
4
votes
3
answers
6938
views
cron lacks permissions to run a script
I have a script (which backs up, using `rclone`) that I run from the Terminal or Automator after `chmod a+x` with no permissions problems. When this script is run in cron (or `launchd`), stderr shows `"/bin/bash: /Users/joshua/script.sh: Operation not permitted."` This approach works fine in Ubuntu....
I have a script (which backs up, using
rclone
) that I run from the Terminal or Automator after chmod a+x
with no permissions problems.
When this script is run in cron (or launchd
), stderr shows "/bin/bash: /Users/joshua/script.sh: Operation not permitted."
This approach works fine in Ubuntu.
Edit: The above question remains relevant, but note that launchd
is the favored cron replacement for Mac, while Automator is available for application-level scheduling.
Joshua Fox
(1387 rep)
Nov 4, 2019, 10:42 AM
• Last activity: Jun 26, 2025, 02:07 AM
2
votes
0
answers
2157
views
How to create a cron log file in macOS High Sierra
I have cron jobs running, but I'm unable to figure out how to make it create a `cron.log` file in `/var/log`. I've edited `/etc/syslog.conf` file by adding: `cron.* /var/log/cron.log` and relaunched the syslog daemon, but I still do not get a log file. I've edited the `com.vix.cron.plist` file and r...
I have cron jobs running, but I'm unable to figure out how to make it create a
cron.log
file in /var/log
.
I've edited /etc/syslog.conf
file by adding:
cron.* /var/log/cron.log
and relaunched the syslog daemon, but I still do not get a log file.
I've edited the com.vix.cron.plist
file and redirected output and error messages to /var/log/cron.log
. Even though that works, it is nowhere near being a proper log file with date and time shown.
user1517199
(21 rep)
Jul 4, 2019, 09:14 AM
• Last activity: Jun 26, 2025, 02:07 AM
0
votes
1
answers
79
views
Execute missed launchd job upon next startup
I would like to make periodic backups using rsnapshot on my MacBook. However, as it is a laptop, it will only be powered on at random times. Thus, scheduled tasks might be missed while it is powered off. A solution for this would be anacron which does not seem to be available on macOS though. Appare...
I would like to make periodic backups using rsnapshot on my MacBook. However, as it is a laptop, it will only be powered on at random times. Thus, scheduled tasks might be missed while it is powered off. A solution for this would be anacron which does not seem to be available on macOS though. Apparently macOS' native launchd service is not capable of scheduling jobs in the manner of anacron. Here it says:
> If you schedule a launchd job by setting the StartCalendarInterval key
> and the computer is asleep when the job should have run, your job will
> run when the computer wakes up. However, if the machine is off when
> the job should have run, the job does not execute until the next
> designated time occurs. All other launchd jobs are skipped when the
> computer is turned off or asleep; they will not run until the next
> designated time occurs.
If my monthly rsnapshot backup is, for example, scheduled for the first day of every month but the laptop is powered off that day, that would mean I lack that backup and only get the next one a whole month later.
Is there any workaround to this? Perhaps linking the launchd schedule with some conditional commands in a bash script?
The only idea I had to solve this was to schedule the launchd job for every single day at a specific time and execute a script which:
1. Checks a timestamp log file if a successful backup has run this month
2. If yes --> exit, if no --> run backup
3. If backup successful --> add timestamp to that file
Though that seems unnecessarily complex in comparison to anacron jobs that you can create on Unix systems.
Daskylos
(1 rep)
Aug 6, 2024, 10:03 AM
• Last activity: May 3, 2025, 05:11 PM
2
votes
2
answers
5286
views
'Operation not permitted' accessing SMB Share as root under Catalina
I have a script that copies a file from our server to the local machine. Here is the script and output + date Thu Jun 11 09:16:00 ACST 2020 + id -un root + pwd /Volumes/Macintosh HD/backups + mkdir /Volumes/Server_auto + /sbin/mount_smbfs smb://User:password@Server/General /Volumes/Server_auto + ls...
I have a script that copies a file from our server to the local machine. Here is the script and output
+ date
Thu Jun 11 09:16:00 ACST 2020
+ id -un
root
+ pwd
/Volumes/Macintosh HD/backups
+ mkdir /Volumes/Server_auto
+ /sbin/mount_smbfs smb://User:password@Server/General /Volumes/Server_auto
+ ls /Volumes
Macintosh HD
Server_auto
+ ls /Volumes/Server_auto
ls: Server_auto: Operation not permitted
Ultimately I want to
cp -f /Volumes/Server_auto/Backups/Web/db_6_1500.zip ./db.zip
but I think if I can at least get a directory listing, the copy will probably work.
I can
this script from the terminal and it runs without a hitch. It has been running for several years from
without a hitch, it's only after updating to Catalina that the problems have started.
I have tried
- adding
,
and
to the Full Disk Access list
- moving the job to a plist file in the LaunchDaemons folder
Everything works just fine except for the copy operation. Behaviour is identical under
or /launchd
.
This is clearly a SIP issue. Before I added cron to the Privacy 'Full Disk Access' list, I was getting the same error message when the cron job started.
Now, it's entirely blocking the external SMB share, but I don't know why or what permission to grant to make it work.
The exact same sequence of operations, when run manually as
, succeed every time.
Any ideas?
Ben McIntyre
(121 rep)
Jun 10, 2020, 06:54 AM
• Last activity: Feb 7, 2025, 06:40 AM
1
votes
1
answers
64
views
Running Cron with PHP on macOS 12 Throws Error - Could not open input file
I have this in my crontab: ```bash * * * * * /Applications/MAMP/bin/php/php7.2.10/bin/php -f /Users/user/Documents/radio_bycron.php 2>&1 > /Users/user/Documents/radio_bycron.php_output.txt ``` In `/Users/user/Documents/radio_bycron.php_output.txt`, there's only one line which is: ```bash Could not o...
I have this in my crontab:
* * * * * /Applications/MAMP/bin/php/php7.2.10/bin/php -f /Users/user/Documents/radio_bycron.php 2>&1 > /Users/user/Documents/radio_bycron.php_output.txt
In /Users/user/Documents/radio_bycron.php_output.txt
, there's only one line which is:
Could not open input file: /Users/user/Documents/radio_bycron.php
I have already chmod 0777
the script /Users/user/Documents/radio_bycron.php
, but I am still getting this issue. The problem happens with or without the -f
flag.
If I execute the command directly on the Terminal, it works perfectly.
How can I solve this problem?
forgodsakehold
(413 rep)
Jul 25, 2024, 01:26 PM
• Last activity: Jan 25, 2025, 10:05 AM
1
votes
2
answers
395
views
ping from script launched by cron
I found that it can not ping from the shell script launched by cron. I wrote shell script like this: ```shellscript #!/bin/bash ping localhost echo "Exit status: $?" >> /tmp/ping.log ``` And I configured crontab so that this script is executed at every minutes. ```shell crontab -e # I wrote `* * * *...
I found that it can not ping from the shell script launched by cron.
I wrote shell script like this:
#!/bin/bash
ping localhost
echo "Exit status: $?" >> /tmp/ping.log
And I configured crontab so that this script is executed at every minutes.
crontab -e
# I wrote * * * * * /path/to/the/script
In some minutes, I checked the file /tmp/ping.log
. According to the log, the status code of ping was 127, indicating it failed.
When I execute ping in usual interactive shell by hand, It succeeds.
Why does it fail from cron ?
hotoku
(33 rep)
Dec 21, 2024, 09:49 AM
• Last activity: Dec 27, 2024, 09:11 PM
2
votes
1
answers
46
views
tar command in backup script throws cannot open destination tgz file when run from cron
I have two external USB hard drives. I have a script that executes tar on one external drive files and puts the tgz another USB external drive. like below : /usr/bin/tar czpf /Volumes/BackupTmp/burocracia-$DATE_STR.tgz /Volumes/Burocracia $DATE_STR is a variable defined at the top of the script. Whe...
I have two external USB hard drives. I have a script that executes tar on one external drive files and puts the tgz another USB external drive. like below :
/usr/bin/tar czpf /Volumes/BackupTmp/burocracia-$DATE_STR.tgz /Volumes/Burocracia
$DATE_STR is a variable defined at the top of the script.
When run from the command line fine. When run from cron of my regular user, the tar commands print :
tar: Failed to open '/Volumes/BackupTmp/burocracia-2024-10-15.tgz'
the cron configuration is :
0 23 */1 * * /bin/bash -c "/Users/dlsa/bin/backup-attached-disks.sh 1> /Users/dlsa/backup-attached-disks.out 2>&1"
I have looked on the Internet, and always sugestions to put it in root's crontab.
But I don't want to do that. I want it to run under my user. Which it is, I've checked in the logs.
Thanks,
Regards
dlsa
(161 rep)
Oct 15, 2024, 11:35 PM
• Last activity: Oct 17, 2024, 08:18 AM
1
votes
0
answers
246
views
Why do I get a deadlock warning for a simple job on a cloud file?
I have a file that sits on a Cloud directory managed by a cloud storage service that uses the Apple [FileProvider][1] API. No, this is not iCloud, but this is apparently the same API framework in use. The desire is that since the API doesn't allow me to pin/favorite a file to prevent it from swappin...
I have a file that sits on a Cloud directory managed by a cloud storage service that uses the Apple FileProvider API. No, this is not iCloud, but this is apparently the same API framework in use.
The desire is that since the API doesn't allow me to pin/favorite a file to prevent it from swapping out to the cloud, I've set up a simple
cron
job that once-an-hour will do this:
cat /Users/myusername/Library/CloudStorage/CloudDrive-Documents/neverswap.txt >/dev/null
I figure that as long as I'm regularly accessing the file, it'll never swap out, right? Unfortunately, it seems like I regularly find this error being spit out by cron:
cat: /Users/myusername/Library/CloudStorage/CloudDrive-Documents/neverswap.txt: Resource deadlock avoided
So my question is:
1. Why is this simple job sometimes resulting in a deadlock? And a deadlock of *what*?
1. I suspect UNIX tools like cat
don't communicate with the FileProvider API and only the Finder seems to know how to trigger things, is this correct?
1. Is there a better way to do this?
thanks
**UPDATE 2024-10-16:** While I haven't found an answer to the above, I will point out that my solution to the cron job was to replace it with a simple Automator script that is two steps: 1/"Run Shell Script" which simply echo
's the name of the file to step 2/"Copy Finder Items" which takes that file and copies it to /tmp
. I then have cron run that app instead of the simple shell command.
bjb
(2664 rep)
Feb 13, 2024, 05:54 PM
• Last activity: Oct 16, 2024, 12:45 PM
5
votes
4
answers
4515
views
How do I know if a time machine backup is running?
Due to [APFS having a mind of its own](https://www.reddit.com/r/mac/comments/75shcg/how_to_disable_time_machine_localmobile_backup/) and filling the hard disk with unwanted snapshots, I made a cron to run `tmutil deletelocalsnapshots xxx`. Later I found out that running this command while a backup i...
Due to [APFS having a mind of its own](https://www.reddit.com/r/mac/comments/75shcg/how_to_disable_time_machine_localmobile_backup/) and filling the hard disk with unwanted snapshots, I made a cron to run
tmutil deletelocalsnapshots xxx
. Later I found out that running this command while a backup is in progress will stop the backup. How can I know if a time machine backup is in progress to avoid running the command? The man page for tmutil
doesn't list any status command.
Right now I'm doing the following in the script, since now backups mount at /Volumes/Time Machine Backups/
:
ls -d /Volumes/Time* 2>/dev/null|grep Machin >/dev/null
if [[ $? != 0 ]]; then
# run here tmutil snapshot deletion commands
fi
However, this doesn't seem to be very precise. The volume may be mounted some minutes later after a backup has started, and also stopping a backup leaves that mounted volume for a while. Finally, the name of the mounted volume might change in the future, making the check useless.
Grzegorz Adam Hankiewicz
(301 rep)
Feb 24, 2018, 06:20 PM
• Last activity: Oct 5, 2024, 11:49 AM
0
votes
1
answers
97
views
what's wrong with my cron command?
In my cron file (Mac, El Capitan) I have a line that simply updates a file name with a date every day. ``` 0 1 * * * mv /Users/me/0backup* /Users/me/0backup_$(date +"\%m-\%d-\%y") ``` This allows me to quickly check that backups have been done. This seems to work fine on other machines, but on this...
In my cron file (Mac, El Capitan) I have a line that simply updates a file name with a date every day.
0 1 * * * mv /Users/me/0backup* /Users/me/0backup_$(date +"\%m-\%d-\%y")
This allows me to quickly check that backups have been done. This seems to work fine on other machines, but on this particular machine it works (the file name DOES get updated), but every day when it is executed I get mailed errors:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
What's going on? Why am I getting these errors?
user618
(167 rep)
Mar 29, 2024, 01:05 PM
• Last activity: Aug 28, 2024, 04:05 AM
4
votes
2
answers
3615
views
How to access Keychain of my user from cron?
`security list-keychains` print: "/Users/vitaly-zdanevich/Library/Keychains/login.keychain-db" "/Library/Keychains/System.keychain" but the same command from `cron` return: "/Library/Keychains/System.keychain" "/Library/Keychains/System.keychain" from `cron` I tried security default-keychain -s "/Us...
security list-keychains
print:
"/Users/vitaly-zdanevich/Library/Keychains/login.keychain-db"
"/Library/Keychains/System.keychain"
but the same command from cron
return:
"/Library/Keychains/System.keychain"
"/Library/Keychains/System.keychain"
from cron
I tried
security default-keychain -s "/Users/vitaly-zdanevich/Library/Keychains/login.keychain-db && security list-keychains"
but result is the same.
In the help of cli-utility security
I did not found any other way to explicitly set the path to the db.
Yes I know that Apple says that better way is to use launchd
instead of cron
, but I сurious to know - it is possible to manipulate my Keychain from cron
?
Vitaly Zdanevich
(231 rep)
Dec 29, 2016, 06:08 AM
• Last activity: May 6, 2024, 07:03 PM
62
votes
5
answers
108015
views
Where is the cron log file in MacOSX Lion?
I want to troubleshoot a cron job that worked fine until a recent modification, but I can't find the cron log file, where is it?
I want to troubleshoot a cron job that worked fine until a recent modification, but I can't find the cron log file, where is it?
Ali
(1269 rep)
Jan 31, 2012, 05:23 PM
• Last activity: Feb 12, 2024, 07:36 AM
1
votes
2
answers
307
views
How can I run a backup script once each weekend on a Mac that sleeps often using launchd?
I recently lost configuration of one of my IDEs (deleted it myself, I am very smart). I had a backup but it was a few months old. I have a script to backup all my config and push it to GitHub. I was looking into a way to run a script every week to back up everything automatically but `launchd` / `cr...
I recently lost configuration of one of my IDEs (deleted it myself, I am very smart). I had a backup but it was a few months old. I have a script to backup all my config and push it to GitHub. I was looking into a way to run a script every week to back up everything automatically but
launchd
/ cron
need a specific time to run. My laptop might not be running at that time but I do boot up my laptop every weekend at least once.
To make it clear, my problem differs substantially from this question talks about running a script "10 minutes after 2 am every weekday only" . I’m not sure how to adapt it to my need to run only once per weekend.
Is there a way to configure launchd
/ cron
to run on a weekend at any time to back up my configuration?
Sourabh
(151 rep)
Jul 21, 2018, 01:36 PM
• Last activity: Feb 4, 2024, 04:06 PM
1
votes
0
answers
135
views
Cron and launchd rsync/ssh job do not work when screen is locked
I have implemented a bash script to perform a backup to a remote server over ssh using rsync. Running the script in the terminal works correctly. I then scheduled a crontab job to run this script twice a week. When I'm logged in, with the screen unlocked, the job runs correctly at the specified time...
I have implemented a bash script to perform a backup to a remote server over ssh using rsync. Running the script in the terminal works correctly. I then scheduled a crontab job to run this script twice a week. When I'm logged in, with the screen unlocked, the job runs correctly at the specified time. However, if the screen is locked some bash commands before the rsync run correctly, but then the rsync connection fails:
xxxxx rsync error: unexplained error (code 255) at io.c(231) [sender=3.2.7]
xxxxx rsync: connection unexpectedly closed (0 bytes received so far) [sender]
I also scheduled it using launchd
and the behavior is the same. Searching a bit I found this might be related to the ssh connection not working when the screen is locked. How can make ssh to work, either with crontab or launchd, even when the screen is locked?
In case is useful information I did have to add this in the bash script to make it work with the ssh agent:
declare -x SSH_AUTH_SOCK=$( ls /private/tmp/com.apple.launchd.*/Listeners )`
The MacOS version I'm using is Mojave 10.14.6. Thank you.
Alan M.
(11 rep)
Dec 20, 2023, 07:14 PM
• Last activity: Dec 21, 2023, 08:20 AM
0
votes
1
answers
122
views
why won't my cron command work?
I have manually added a command to my `crontab` file (Monterey 12.6.9). Pretty simple; the `crontab` entry is something like: ``` 0 10 * * * mv /Users/me/Desktop/test /Users/me/Desktop/test2 ``` Which means, at 10am every day, do this rename. But it simply doesn't work. Other commands in my `crontab...
I have manually added a command to my
crontab
file (Monterey 12.6.9). Pretty simple; the crontab
entry is something like:
0 10 * * * mv /Users/me/Desktop/test /Users/me/Desktop/test2
Which means, at 10am every day, do this rename.
But it simply doesn't work. Other commands in my crontab
file work fine, which suggests that cron
IS actually executing. I HAVE given the cron
app "Full Disk Access" in Security & Privacy. That mv
command works fine as a terminal command. Not sure where the cron log is, so I haven't pieced together what is happening. This is confusing. Advice?
user637
(11 rep)
Sep 20, 2023, 12:39 AM
• Last activity: Sep 20, 2023, 02:30 AM
5
votes
0
answers
459
views
Add "Allow in the background" items from command line
Some of the applications on my system run only if they are allowed to run in the background. I want to run these apps only in certain time windows. Due to this, I am manually enabling the app in `System Settings` -> `Login items` -> `Allow in the background` and then once I am done with using the ap...
Some of the applications on my system run only if they are allowed to run in the background. I want to run these apps only in certain time windows.
Due to this, I am manually enabling the app in
System Settings
-> Login items
-> Allow in the background
and then once I am done with using the app, I am disabling it.
I want to write a cron job to automatically enable/disable items in the background.
Is there a way to do that from command line or osascript?
Chillar Anand
(263 rep)
Sep 6, 2023, 07:48 AM
0
votes
0
answers
131
views
Python script unable to access external drive under cron
My desired cron job consists of a bash script that does the following: * Create a new Python environment (with a new python executable) * Use that Python executable to run a Python script, which accesses an external hard drive How do I create a job like this on macOS? My issue is that cron and execu...
My desired cron job consists of a bash script that does the following:
* Create a new Python environment (with a new python executable)
* Use that Python executable to run a Python script, which accesses an external hard drive
How do I create a job like this on macOS?
My issue is that cron and executables under cron do not have access to external drives by default. I typically work around this by manually allowing "Full Disk Access". For example, I have given my cron and rsync executables full disk access already, which solved their respective issues with disk access.
However, I am unable to give the python executable full disk access manually since I am creating a new python executable for each job that runs.
It would be great if I could programmatically give my new python executable access to the external drive, or just disable the need for "Full Disk Access" entirely, but I haven't found a way to accomplish either of those.
Michael Jan
(1 rep)
Jul 24, 2023, 07:11 PM
• Last activity: Jul 24, 2023, 09:29 PM
2
votes
1
answers
310
views
What timezone is cron in?
If I run `crontab -e` and add a cronjob `4 5 * * * "echo hi"`, the job will run at 5:04 of what timezone? Let's say the laptop displays time in PST in the top menu bar and `sudo systemsetup -gettimezone` is `Time Zone: America/Los_Angeles`. I tried running a test job to find the answer, but the job...
If I run
crontab -e
and add a cronjob 4 5 * * * "echo hi"
, the job will run at 5:04 of what timezone? Let's say the laptop displays time in PST in the top menu bar and sudo systemsetup -gettimezone
is Time Zone: America/Los_Angeles
.
I tried running a test job to find the answer, but the job didn't run when I expected in both UTC and PST. Probably a simple misconfiguration, but I'd like to confirm what timezone I should expect first.
Almenon
(303 rep)
Jul 17, 2023, 11:18 PM
• Last activity: Jul 18, 2023, 07:18 PM
67
votes
1
answers
36900
views
Crontab Operation not permitted
I have the following crontab on my mac OS Catalina 10.15.1 : ``` * * * * * /Users/username/Desktop/cronjob.sh > /Users/username/Desktop/cronjob.log 2>&1 ``` Within the command file is: ``` #!/bin/bash touch /tmp/foo ``` However I was left with the following line inside `cronjob.log` ``` /bin/bash: /...
I have the following crontab on my mac OS Catalina 10.15.1 :
* * * * * /Users/username/Desktop/cronjob.sh > /Users/username/Desktop/cronjob.log 2>&1
Within the command file is:
#!/bin/bash
touch /tmp/foo
However I was left with the following line inside cronjob.log
/bin/bash: /Users/username/Desktop/cronjob.sh: Operation not permitted
---
Why is operation not permitted event though the permissions for cronjob.sh
is set to +x
?
Tian
(995 rep)
Dec 29, 2019, 10:57 AM
• Last activity: Jul 11, 2023, 01:45 AM
Showing page 1 of 20 total questions