I'm trying to write a script that opens a brand new instance of safari, displays a webpage for 10 minutes, then quits safari.
I think I can follow this Stack Exchange to do steps 1 & 2 of my goal.
For the "display 10 minutes piece," I wasn't sure how to do that except to schedule a separate cron job to quit safari that executes 10 minutes after the first script.
Any idea on how to write a cron script to quit safari? Or to make this process easier? I'm a newbie at cron and would love to learn more.
Thanks!
Best Answer
As an example, the following commands can be used in a shell script to accomplish what you've expressed.
Notes:
Obviously you'd change the value of the URL in the
open
command.open -na
- Assuming another instance of the target app is running, opens a new instance of the target app, and if not, just opens the target app.pgrep -xn
- Gets thepid
of the newest instance of the target app, so you know which instance to terminate.pid=$(...)
- Command Substitution,$(command)
, allows the output of a command to replace the command name. In this case, assigns thepid
of the target app to the variablepid
for use as$pid
with thekill
command.sleep 600
- Sleeps for 10 minutes.kill $pid
- Terminate the process by itspid
.I recommend you read the manual pages for
open
,pgrep
,sleep
andkill
. You can read the manual page forcommand
in Terminal by typingcommand
and then right-click on it and select: Open man Pagecron
example:From the manual page for
crontab
:I'm mentioning this because I personally prefer to use
launchd
overcron
as it's the preferred method that Apple endorses.If you want to use
cron
, have a look at Scheduling Jobs With Crontab on macOS and crontab guru.I tested a shell script with the above commands and triggered it both as a
cronjob
and as a launch agent under macOS Catalina, and it worked for me as coded.To create the
crontab
, I did the following in Terminal:crontab -e
because I prefernano
over the defaultvim
.In
nano
I added the following:Then pressed ^O to save and ^X to close
nano
.As set,
cron
ran the job at 6:30 PM executing the/usr/local/bin/codetest
andcodetest
had a#!/bin/zsh
shebang and the commands above, at the start of this answer, in it.Notes:
I recommend you read the manual pages for
cron
andcrontab
. You can read the manual page forcommand
in Terminal by typingcommand
and then right-click on it and select: Open man PageFrom Scheduling Jobs With Crontab on macOS:
Setting cron jobs requires a specific format.
You can also use sites like Crontab.guru to generate cron expressions.
launchd
example:For
launchd
, have a look at: Creating Launch Daemons and AgentsAs a launch agent, in Terminal:
I then added the following:
Saved the document and then back in Terminal:
Then at 6:45 PM
launchd
executed the/usr/local/bin/codetest
shell script.Notes:
I recommend you read the manual pages for
launchctl
,launchd.plist
andlaunchd
. You can read the manual page forcommand
in Terminal by typingcommand
and then right-click on it and select: Open man PageOnce loaded, it will automatically load when you login to your account, so you shouldn't need to do it manually again under normal circumstances. To unload it, use the
unload
subcommand with thelaunchctl
command.After unloading it, you can delete the .
plist
file, if/when you want to undo changes you've made to your system.