Skip to content

Run Periodic Script Using launchd on iOS

Liza Babu edited this page Aug 24, 2020 · 5 revisions

launchd can be used to continuously run a script or a program on iOS. This works similar to creating a cron job on a Linux based machine. Natively, the cron daemon does not exist on iOS.

First create a .plist file in /Library/LaunchDaemons/. This file will contain information about the job such as what program should be run, its arguments or how often to run it. This file has an xml format.

Below I describe the keys added to the file in order to make it work. The entire set of keys can be found here.

  • The label should be a unique identifier for your running job
<key>Label</key>
<string>com.run.lsof</string>
  • The user unde which you want the program to run. I used the root user.
<key>UserName</key>
<string>root</string>
  • The program arguments. Use absolute paths here.
<key>ProgramArguments</key>
  <array>
    <string>/bin/bash</string>
    <string>/var/root/run_lsof_and_log.sh</string>
  </array>

On macOS this works without running a /bin/bash with the first argument set to my script, on iOS I didn't manage to get it working. I describe below the error I got when not setting up a /bin/bash.

  • The standard input, output and error can be set to custom files. I only set the standard output and error since my script did not require any user input. Important to know is that launchd will append any output/error to these files and they will not be overwritten by each run of the program.
<key>StandardErrorPath</key>
<string>/var/root/run_err.log</string>
<key>StandardOutPath</key>
<string>/var/root/run.log</string>

The output and error files specified above are the ones where the output and errors occured while running your script/program, not while loading or starting it.

  • A key to specify that you want the program to be relaunched if any sort of error occurs.
<key>KeepAlive</key>
<true/>
  • A key to specify that you want your script to run for the first time right after loading.
<key>RunAtLoad</key>
<true/>
  • The period of time that should pass between consecutive runs on my program. Below I set it to run every 2 seconds.
<key>StartInterval</key>
<integer>2</integer>

After creating a new .plist in /Library/LaunchDaemons/ you have to load it so that the launchd daemon knows about it. There should be not output after running this command. Then start it.

# launchctl load /Library/LaunchDaemons/com.run.lsof.plist
# launchctl start /Library/LaunchDaemons/com.run.lsof.plist

Check if everything is ok.

# launchctl list | grep lsof
-  0  com.run.lsof

0 means that no error has occured. Status code 75 means, according to the manual, Function not implemented. I was getting the status code when the .plist file had a misconfiguration. To be exact, the key I was using was Program instead of ProgramArguments and I wasnt' specifying to launch the srcipt as an argument to /bin/bash.

If you want to stop this, simply unload the task from launchd.

# launchctl unload /Library/LaunchDaemons/com.run.lsof.plist

That's all. Enjoy!

Clone this wiki locally