1

I am learning how to create a Launch Daemon. This job aims to create a text file on on the user's desktop on boot up. The plist file is placed in Library/LaunchDaemons/. Here is the plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.test.createFile</string>
    <key>Program</key>
        <string>/Users/hlocng/Desktop/createFile.sh</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

... and here is the bash script:

#!/bin/bash
touch /Users/hlocng/Desktop/newFile.txt

I tried launchctl load /Library/LaunchDaemon/com.test.createFile and there was no error but no file is created, not even when I reboot the computer. I do not know what I am doing wrong. Any help is appreciated. Thanks you!

Fried Rice
  • 3,295
  • 3
  • 18
  • 27
  • 1
    Is the script marked executable? Have you tried specify the program to be the interpreter (`/bin/sh`) and the script file as its argument? – Ken Thomases May 20 '14 at 04:42
  • I ran the script file alone from the terminal and it worked fine. I alse tried `ProgramArguments filePath ` – Fried Rice May 20 '14 at 06:02
  • 2
    Since this is a launch daemon in `/Library`, I believe that a) the property list file must be owned by root, and b) you need to use `sudo launchctl ...` to load it. Also, it's not clear if you tried my second suggestion in my previous comment (using `/bin/sh` explicitly). – Ken Thomases May 20 '14 at 06:15
  • Thanks for the quick reply, I tried `launchctl load /Library/LaunchDaemon/com.test.createFile` again and it worked magically with both `/bin/sh` and `/bin/bash`, however restarting the computer still did not create the file. I tried `sudo launchctl ...` like you have suggested and I got this message `launchctl: Dubious ownership on file (skipping): com.test.createFile nothing found to load`, seems like you are right about the file ownership. Though, I do not know how to achieve this, can you please tell me how to give file ownership to root? – Fried Rice May 20 '14 at 07:43
  • I have fixed the permission and everything worked, thanks Ken! – Fried Rice May 20 '14 at 08:11

1 Answers1

1

The permission of the plist file must belong to root. ls -l can be used to check the permission. sudo chown root:wheel filePath changes the permission to root and everything worked fine. Thanks to @Ken Thomases!

Fried Rice
  • 3,295
  • 3
  • 18
  • 27