6

I have the following code in a text file (myfile.applescript):

#! /usr/bin/osascript
tell application "Google Chrome" to quit

I would like Chrome to shut down each night at 9:15PM, so I added the following cron task to crontab:

15 21 * * * ~/Desktop/myfile.applescript

Unfortunately, the script is not triggering. Any thoughts?

NOTE: I am aware I can use iCal to resolve this problem. However, I'd prefer to not clutter my calendar with a daily script-triggering event.

4 Answers 4

4

Make sure the ~/Desktop/myfile.applescript file is executable. You can only call a script by filename if it has the relevant execute bit.

Consider the following Terminal session:

$ ls -laFh sh.sh
-rw-rw-r--  1 jason  staff   278B Mar  2 10:18 sh.sh

$ ./sh.sh
-bash: ./sh.sh: Permission denied

$ chmod 764 sh.sh

$ ls -laFh sh.sh
-rwxrw-r--  1 jason  staff    32B Mar  7 09:40 sh.sh*

$ ./sh.sh 
Hello World!

Until I had execute permissions (denoted with an 'x' in either the 4th, 7th, or 10th slot of the permissions string), I could not run it.

You can override it by calling the file with it's particular interpreter:

$ chmod 664 sh.sh

$ ls -laFh sh.sh
-rw-rw-r--  1 jason  staff    32B Mar  7 09:40 sh.sh

$ ./sh.sh
-bash: ./sh.sh: Permission denied

$ sh sh.sh
Hello World!
1
  • I'm sorry about the terrible filename. It was a temporary file so I gave it a stupid filename. But "sh sh sh" all in a row is very absurd. And for the record: I highly suggest using launchd, cron is easier but launchd is more complete. Apps like Lingon that jaberg referenced really do help to make a functional launchctl plist in a hurry. Commented Mar 7, 2012 at 16:46
3

Cron has be deprecated on modern versions of OS X. Apple's suggestion is to use launchd instead. You will find Lingon to be a useful tool for creating launchd daemons.

If you would prefer to use iCal to launch the script, you can create a separate calendar for scripted event(s), and hide it from view. It will function, but your calendar view will not be cluttered.

enter image description here

0
2

I discovered my original script worked. The problem had to do with my cron entry: it wasn't terminated with a carriage return. Once I did that, the applescript triggered properly.

1

Try the following in your script.

#!/usr/bin/bash
/usr/bin/osascript -e 'tell application "Google Chrome" to quit'

Make sure it's chmod +x and test from CLI.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .