Close Apps After A Period Of Time 4

Terminal

Share
Close Apps After A Period Of Time

Hey

Here is a cool little trick that any coders and or Terminal users may find useful. If you want any script to wait for a period of time before it closes an application, this is the command you should use. Its really simple, there is probably better solutions to this problem, although this is quick and dirty.

To cause any bash script or Terminal script to wait for a period of time you can use the ping command. All this does it ping your own computer for a lot amount of time. After the ping has finished it will then run the next command. For instance you could use:

ping -c 10 localhost >nul; killall Mail

This will wait ten seconds before it will kill Mail. There will be no output on the screen at all so it can run silently.

If you want a safer approach to closing apps you can use this line.

ping -c localhost >nul; osascript -e "tell application \"Mail\" to quit"

This will tell applescript to close the application instead of using Terminal.

This is good little tip to build into any bash scripts you are building.

I’m sorry for the lack of posts and this short one. Univeristy work is at an all time high and the end of semester is getting close so I am packed. I will however try and do a couple of posts a week.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

4 Responses to “Close Apps After A Period Of Time”

  1. 1

    You can also use:

    sleep 10; killall Mail

    Comment By Pip on April 29th, at 10:26 pm

  2. 2

    can’t you just use sleep? as in: sleep 10

    Comment By anon on April 30th, at 6:37 am

  3. 3

    Thanks for the trick

    Comment By UHB: Unidentified Human being on April 30th, at 10:07 am

  4. 4

    Wasting cpu cycles on pinging is unnecessary. as suggested by above users, sleeping is a better way out.. !!

    Comment By Jay on May 4th, at 3:05 am