-1

I want to stop browsing after midnight and in order to do so:

  1. I've made at /Users/sergeart/Desktop file called goToSleep.sh:

    pid=$(pgrep -xn "Safari") kill $pid

  2. I've created crontab (with crontab -e) where I put

    0-59 0-6 * * * bash /Users/sergeart/Desktop/goToSleep.sh

I expect cron will run goToSleep.sh script every minute after midnight till 6am, but unfortunately Safari is still working.

What do I miss?

  • Yep. I works just fine when I run it in the terminal :( The first command gets safari process id and the second - kills it:) – sergey artyukhov Aug 21 '22 at 21:57
  • 1
    Your Desktop folder (like a number of other places in your home directory) has privacy protection restrictions in addition to the usual permissions, and by default cron jobs don't have access to it (see [this](https://apple.stackexchange.com/questions/332673/what-and-how-does-macos-mojave-implement-to-restrict-applications-access-to-pers) and [this](https://stackoverflow.com/questions/73256814/macos-crontab-runtime-stderr-usr-local-bin-python3-cant-open-file-myscript)). Try putting the script somewhere other than your home directory. Also, please check the code formatting in your question. – Gordon Davisson Aug 21 '22 at 22:22

1 Answers1

1

Note that

foo=something command $foo

is not at all the same thing as

foo=something
command $foo

The first one does not call command something but instead calls command $foo (for the old value of foo) with something being assigned to the environment variable foo. The second one calls command something but does not change the environment variable foo.

In your specific case, I'm not sure why you even bother with a variable, you should be able to simply do

kill $(pgrep -xn "Safari")
Guillaume Brunerie
  • 4,676
  • 3
  • 24
  • 32