Run the script shell periodically on Mac OS X without root permission

I want to run a file with type .sh or .py on mac os x without using root, I searched on google and found that launchctctl can help me,

so I read the tutorial and do the same in the tutorial, but it does not work for me, [I use mac os x 10.9 x64]

My .plist file [run the 1.sh file every 60 seconds]:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.alvin.crontabtest</string> <key>ProgramArguments</key> <array> <string>/Users/paul/Desktop/1.sh</string> </array> <key>Nice</key> <integer>1</integer> <key>StartInterval</key> <integer>60</integer> <key>RunAtLoad</key> <true/> <key>StandardErrorPath</key> <string>/tmp/AlTest1.err</string> <key>StandardOutPath</key> <string>/tmp/AlTest1.out</string> </dict> </plist> 

1.sh source:

 echo '+' >> /Users/paul/Desktop/worked.txt 

I put Run.plist in /Users/paul/Run.plist

and execute the command from the terminal:

 launchctl load /Users/paul/Run.plist Launchctl start com.alvin.crontabtest 

commands are executed without errors, but I do not see anything in work.txt

can someone help me?

+8
bash cron startup macos launchd
source share
2 answers

Any specific reason why you don't want to use regular crontab?

 % echo "* * * * * /Users/paul/Desktop/1.sh" | crontab - 

This command should add a cron job that runs once a minute.

Indicate that this command will also replace any existing crontab. crontab - command crontab - should be used with care as a short string.

If you want to edit an existing crontab to avoid erasing previously defined jobs, you can use crontab -e . (If it starts vim , and you don't know how to use vim , you can exit by pressing ESC : q ! Enter , and then go to the documentation for the search.)

If you need instructions on editing crontabs, enter man crontab in your shell. If you need syntax information in a crontab file, man 5 crontab will show you this.

Enjoy it!


UPDATE: (for each comment)

A simple hack is required every 30 seconds to complete your work. Cron only runs jobs in a minute, so you can have two jobs to run every 30 seconds, one of which has a delay of 30 seconds. For example:

  #Mn Hr Da Mo DW Command * * * * * /Users/paul/Desktop/1.sh * * * * * sleep 30; /Users/paul/Desktop/1.sh 

Hope this helps.

+6
source share

To clarify: the OP .plist file .plist was perfectly in order - the problem was inside the script shell called (not shown in the question).

In OS X, using .plist files downloaded by the launchctl CLI and invoked by the launchd daemon manager is the preferred way to schedule (recurring) tasks (see below for more details).

Notes:

  • The launchd .plist file .plist described in https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html or man launchd.plist
  • For the .plist file that will be loaded every time the current user logs in , it must be placed in ~/Library/LaunchAgents/ (all users' files must be placed in /Library/LaunchAgent/ - requires root privileges).
  • Specifying output capture files with the keys StandardOutPath and StandardErrorPath means that subsequent calls are added to the specified files, which means that these files continue to grow indefinitely, unless they are managed externally .
  • Re troubleshooting : @Grady Player tip: launch Console.app and search for entries com.apple.launchd.peruser - failure to call the command specified in .plist will show there.

@ghoti's answer describes a general Unix alternative for launchd , cron (commonly used on Linux):

Regarding how cron relates to OS X: @ghoti asks:

Any specific reason why you don't want to use regular crontab?

In OS X, man crontab advises (highlighted by me):

Although cron (8) and crontab (5) are officially supported under Darwin [OS X], their functionality has been absorbed into launchd (8) , which provides a more flexible way to automatically execute commands . See the launchctl (1) article for more information.

Bottom row such is:

  • If you came from * nix background , it might be more convenient for you to continue using cron and crontab , assuming:
    • you are aware that there may be additional background tasks scheduled with launchd .
    • you know the limitations of cron and can work with them / around them.
  • Otherwise on OS X :
    • many third-party applications use the built-in launchd function and thus set periodic background tasks via .plist files in /Library/LaunchAgents (for all users) or ~/Library/LaunchAgents (for the current user).
    • If you want to centralize the management of background tasks in these places and / or want to take advantage of the increased flexibility provided by launchd , specify the task of background tasks via .plist files evaluated by launchd .

Adding simple cron tasks is probably easier than creating .plist files for launchd , but third-party utilities like Lingon 3 can help with the latter.

In addition, there are subtle differences in how cron tasks are called up against tasks for the launchd user: for example, the former do not allow the user to interact through AppleScript, while the latter do.


The case in terms of increased flexibility launchd : OP, in a subsequent comment, asks the task to run every 30 seconds:

  • The minimum interval for cron is 60 seconds , which requires a workaround in @ghoti's answer.

  • Unlike the launchd .plist file, just change <key>StartInterval</key><integer>60</integer> to <key>StartInterval</key><integer>30</integer> .

+20
source share

All Articles