0

I have a bash script like so:

#!/bin/bash

cd /
pwd
ls

cd Users
pwd
ls

cd robl
pwd
ls

cd Documents
pwd
ls

When I run it manually via ~/bin/script.sh, it works as expected.

However, when I make a .plist, schedule it and start it it using launchctl like so:

launchctl load ~/Library/LaunchAgents/com.robl.script
launchctl start com.robl.script

It fails. Specifically, it runs as expected until the last ls, on which it throws the error

ls: .: command not found

I think this may be some kind of permission issue since the script works when running manually but not via launchctl. Especially because all of the ls‘s work and print the contents, except for the last one in the Documents folder.

Any ideas?

robl
  • 74
  • 7
  • I've never used `launchctl` myself but I just read the `man` page for it. Are you sure you are using it correctly? – Ted Lyngmo Apr 29 '22 at 18:33
  • I think so. I followed the following post, and it works when I manually run the job using launchctl start, verified since it’s outputting most of the commands https://stackoverflow.com/a/49959004 – robl Apr 29 '22 at 18:34
  • I don't see the connection with that post and what you've shown. `ls: .: command not found` looks like you managed to get supply `ls` as the name to the shell so that it's really `bash` that is saying `.` `command not found` as if `.` is the command to be executed. – Ted Lyngmo Apr 29 '22 at 18:38
  • 1
    That error doesn't make much sense, but you might be running into trouble with the privacy protections in macOS (your Documents folder is considered a private area). See ["Why do I get "ls: Desktop: Operation not permitted" when I own Desktop?"](https://apple.stackexchange.com/questions/425519/) and ["How to run a LaunchAgent that runs a script which causes failures because of System Integrity Protection"](https://apple.stackexchange.com/questions/338213/). – Gordon Davisson Apr 29 '22 at 18:40
  • Sorry if I wasn’t clear - when I execute the job via ‘launchctl start com.robl.script’, I know the script is executing as expected up until the last ‘ls’ because the rest of the commands are being output as expected. When I execute the job and look at the output file, the contents of all commands except the last ‘ls’ are shown as they should be. – robl Apr 29 '22 at 18:42

0 Answers0