Automatically Run a Script On iPhone 4
If you need to automatically run a script on iPhone 4 through certain periods of time here’s what you need to do. Since cron daemon is not working on iPhone starting from the version 3.0 you can achieve the task by using the launchd daemon. This a preferred way to add a timed job as each launchd task is managed by a specific file. This means that each timed task can be managed just by adding and removing the file.
Here’s the link to the official Scheduling Timed Jobs on Apple’s developers community: Launchd on Apple’s Mac OS X Developer Library
As you have the running script, it’s better to test it on iPhone’s shell before running that piece of code. The script to be launched could be like this:
Then you have to go to /System/Library/LaunchDaemons/and create a .plist file there. You can name it as you wish. The newly created .plist file should look like this:
<?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>org.me.hello</string> <key>ProgramArguments</key> <array> <string>/var/root/scripts/hello</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>300</integer> </dict> </plist>
Choose the time interval you want to set for the executable task between the <integr></integr> tags.
After creating the file you have to ask the system to launch it:
iPhone4:~ root# launchctl load /System/Library/LaunchDaemons/your_plist_name.plist
The script will be now launched with 300 seconds intervals.
The way one can do this is pretty simple so every school kid can do that. If you miss your favorite cron daemon so much, this solution is exactly for you. Even official Apple’s dev community recommends to run launchd daemon instead of cron for purposes like this.
Please mention that if the device is turned off the script won’t execute until gadget is turned back on. The next task will be performed at the scheduled time.
Tags: cron, iphone hacks, launchd, script