How do I make iTerm terminal notify me when a job/process is complete? - macos

A notification center notification would be ideal but growl, bounce dock, sound, etc would be fine, too (or if this can only be done in Terminal.app I'd be willing to switch back). Is there an option somewhere in iTerm to turn on notifications or is it something I'm supposed to type at the end of a command in the terminal? If the latter, is it possible to add an alert once process has started (for example if I realize it's going to take longer than I initially expected, I'm bad at guessing).

Notify on an already running process:
Edit → Marks and Annotations → Alerts → Alert on next mark
or Shortcut: ⌥⌘A
: iTerm will literally keep an eye (on top right corner) of your terminal. Once the command is finished, it will issue a notification.
Requirement: Shell Integration:
iTerm2 → Install Shell Integration
NOTE: integration will not issue notifications until iTerm2 is restarted.
Example Use Case:
We launched a command, underestimated completion time, and we don't want to cancel or just wait for completion.

And you can always use the say command.
Usually when you are running a long process inside the terminal and want to get updated you can simply use this command to speak out things like done or error or bazinga.
mvn clean install; say done
This command builds a java spring app, and takes a long long time, and it will speak out done after the process is complete.

You can add any one of the following after any command, with a semi-colon in between the command and it:
afplay /System/Library/Sounds/Ping.aiff -v 2
osascript -e 'beep 3'
tput bel
or, if you like Notification Centre
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
You can also make an alias in your profile, called notify and add that at the end of your command. So, in your login profile
alias notify="tput bel"
then
sleep 10; notify
Or, if you started your command and it is "hanging", just type notify and hit Enter and it will run your notify alias at the end, whne the command has finished, e.g.
sleep 20
# wait 5 seconds before realising this will take 20 seconds
notify<Enter>

iTerm2 supports Growl notifications. You can turn it on in each profile settings.
Select a profile in Preferences…->Profiles.
Then in Terminal tab there is an option Enable Growl Notifications.
Remember to also enable iTerm notifications in Growl preferences.
If you want to get notification for a given process you could try to experiment with Triggers. You define triggers in Advanced tab in a profile settings. In this way you may assign a Growl notification to a particular output of your process (regexp).
You could for example do:
$ mycommand; echo "end-of-my-process"
And connect trigger to "end-of-my-process" message.
Update
Read more about triggers on iTerm2.com.

There is an OSS tool called noti.
You can easily install it with brew install noti and start using it just by prefixing your command with noti like noti sleep 3.

Install the iTerm2 shell integration
curl -L https://iterm2.com/shell_integration/install_shell_integration_and_utilities.sh | bash
Execute your command and concatenate the attention app, e.g.
./task && ~/.iterm2/it2attention once
It'll cause the iTerm app to bounce it's icon once the job is complete.
You also have other attention options:
$ .iterm2/it2attention -h
Usage:
it2attention start
Begin bouncing the dock icon if another app is active
it2attention stop
Stop bouncing the dock icon if another app is active
it2attention once
Bounce the dock icon once if another app is active
it2attention fireworks
Show an explosion animation at the cursor

You can also use terminal-notifier which use mac os system notifications. To install it via Home brew just:
$ brew install terminal-notifier
Then if you want to display notification when your job/process is done use something like this
$ <your job/process command> && echo 'Completed' | terminal-notifier -sound default
And this display like this:
You can also change sound and icon of notifications. More info in github repo: https://github.com/julienXX/terminal-notifier

Related

Screencapture over cron shown background instead window content

On macOs Catalina have problems with making screenshots over cron.
When manually run do_screenshot.sh script then all fine.
But when it run auto over cron - probs, only menu correct, instead window content shown macOs background(see pic)
do_screenshot.sh:
#!/bin/bash
DATEFULL=`date '+%Y%m%d%H%M%S'`
FILENAME="/Users/yak/Documents/screenshots/"$DATEFULL.png
/usr/sbin/screencapture -x $FILENAME
Ran into this problem after the update too. Spent hours learning the details, here is the why and how. Credit goes to big-circle. The original question and answer here.
The problem is the cron doesn't have screen access.
Here's the solution
close SIP (System Integrity Protection).
Make sure your SIP is disabled. To check if SIP is disabled. To to terminal and type csrutil status. It should say SIP status: enabled/disabled.
To disable it:
Powerdown Mac, start up again and hold down cmd+r until OS X Utilities window shows up. Open terminal type csrutil disable. Restart again, boot into normal mac os.
grant write permission to TCC
sudo chmod 664 /Library/Application\ Support/com.apple.TCC
grant screencapure privilege to cron and screencapture
a) CRON:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/cron", 1, 1, 1, "", "", "", "UNUSED", "", 0,"")'`
b) screencapture:
Pre Big Sur:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/screencapture", 1, 1, 1, "", "", "", "UNUSED", "", 0,"")'`
--------------------------------------------------------------------------
Big Sur and later:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/screencapture", 1,2,4,1, "", "", "", "UNUSED", "", 0,"")'`
It's probably a good idea to turn SIP back on at this stage. To enable it, follow step 1, and instead of csrutil disable just type csrutil enable.
Edit # 2021-12-09T11:58:00+1000: Added in commands for Big Sur and later per comment from Silvan Mühlemann
You should allow your script to use the "Screen Recording" in the "Security & Privacy" system preferences.
You may start your app in a background from a terminal with a long loop (you need start it manually, that is not good):
#!/bin/bash
for value in {1..980}
do
/Users/<username>/record_activity_with_screen_capture.bash
sleep 60
done
echo All done
OR
use launchd (instead of cron) for running this script.
There are two examples:
how to start bash script in launchd
How to add timing
calendar example
Instead of playing with SIP (System Integrity Protection), which is dangerous, but suggested anyways but the top answer, simply give Screen Recording permissions to /usr/sbin/cron (or /sbin/cron in some MacOS versions I think).
In order to do that:
Open System Settings
Open Security & Privacy
Go to the Privacy tab
Go to Screen Recording
Click the lock in the bottom left corner and authenticate in order to enable editing the list
Now, if cron is listed there and unchecked, check it and the job is done. Likely is not listed there at all and you need to add it manually
Click on + below the list of programs
In the file picker hit a magic command Cmd+Shift+G, which will show a Go To Folder popup
Type: /usr/sbin and hit Enter (if you can't find cron in the next steps, try /sbin here instead)
You should see a list of files, all with an icon of a black terminal
Find cron on the list and hit Open
Check the checkbox next to cron
Wait for your cron to run the command again.
Voila, it should be capturing screenshots now.
You can try to change shebang to #!/bin/sh, or change screencapture to exec screencapture

How to enter keyboard shortcuts in bash script

Is it possible to enter keyboard shortcuts into a bash script? For example, I have tried typing in ^d to substitute for control + d (logout) without success.
ubuntu#vfleet:~$ ^d
-bash: :s^d: substitution failed
ubuntu#vfleet:~$ ^D
-bash: :s^D: substitution failed
I am using screen to run a django development server in the background. One of the commands to run a particular screen as a daemon is control + a + d. The goal is to be able to enter in control + a + d into a bash script. For example:
python manage.py runserver
^a + d
Is this possible?
Edit:
A valid method of avoiding the keyboard shortcut in screen is linked by Eric Renouf in the comments below. screen -d -m sh -c "python manage.py runserver" will start a development server as a daemon. This is a great solution for my particular problem, but it would still be nice to have a solution for the original question at hand.
xdotool package is the solution here.
xdotool key ctrl+d
Full reference
You probably should just start the command without attaching to the screen session in the first place, like
screen -d -m python manage.py runserver
but if you can't do that for some reason, you could detach from a screen session you're currently in by doing:
screen -S "$STY" -X detach
screen saves its current session info in STY, so we'll use that to make sure we're interacting with the correct session (in case there are many). Then we'll use -X to send a command to that session, in this case our command will be detach which will detach all the attached sessions, including the one used to execute that command
So while this doesn't actually send key strokes, it does highlight that there is often another command that you can send to accomplish your goals. Here detach takes the place of ctrl+a+d. Sending quit or running exit could often replace ctrl+d.
Another work-around would be to use expect which you could then use to send the strings containing control characters or hex values of them.

Pass command from scheduled script to program running in xterm window

I have a game server running in an xterm window.
Once daily I'd like to send a warning message to any players followed after a delay by the stop command to the program inside the xterm window from a script running on a schedule. This causes the cleanup and save functions to run automatically.
Once the program shuts down I can bring it back up easily but I don't know how to send the warning and stop commands first.
Typed directly into xterm the commands are:
broadcast Reboot in 2 minutes
(followed by a 2 minute wait and then simply):
stop
no / or other characters required.
Any help?
Do you also need to type something from the xterm itself (from time to time) or do you want your server to be fully driven from external commands?
Is your program line-oriented? You may first try something like:
mkfifo /tmp/f
tail -f /tmp/f | myprogram
and then try to send commands to your program (from another xterm) with
echo "mycommand" > /tmp/f
You may also consider using netcat for turning your program to a server:
Turn simple C program into server using netcat
http://lifehacker.com/202271/roll-your-own-servers-with-netcat
http://nc110.sourceforge.net/
Then you could write a shell script for sending the required commands to your "server".
If you know a little about C programming; I remember having once hacked the script program (which was very easy to do: code is short) in order to launch another program but to read commands from a FIFO file (then again a shell script would be easy to write for driving your program).
Something you might try is running your program inside a screen session.
You can then send commands to the session from cron that will be just
as if you typed them.
In your xterm, before launching the program do:
screen -S myscreen bash
(or you can even replace bash by your program). Then from your cron
screen -S myscreen -X stuff 'broadcast Reboot in 2 minutes\n'
sleep 120
screen -S myscreen -X stuff 'stop\n'
will enter that text. You can exit the session using screen -S myscreen -X quit
or by typing ctrl-a \.
screen is intended to be transparent. To easily see you are inside screen, you can
configure a permanent status bar at the bottom of your xterm:
echo 'hardstatus alwayslastline' >~/.screenrc
Now when you run screen you should see a reverse video bottom line. Depending
on your OS it may be empty.

Is it possible to log Growl 1.2.2 notifications?

Essentially I want to send my phone a pushover alert when a growl happens.
To do this I am hoping to have a hook for when a growl is fired or failing that a log file I can just scan on a cron job. Then I can use either of those to send the pushover alert.
Does growl 1.2.2 have the ability to offer a hook or log it's growls?
Yes, and the answer appears many places with, often the same error(*) copied. I just set it up myself, so here:
Decide where you want your log file. Examples all indicate it must exist first, though I did not verify...
growlog="$HOME/Library/Logs/Growl.log"
touch "$growlog"
check existing growl settings, before modifying:
defaults read com.Growl.GrowlHelperApp
These are the magic settings:
defaults write com.Growl.GrowlHelperApp "Custom log history 1" "$growlog"
defaults write com.Growl.GrowlHelperApp GrowlLogType 1
defaults write com.Growl.GrowlHelperApp GrowlLoggingEnabled -bool YES
go to the growl 'paw' in the menu line and 'restart' -- after this I saw a line in the file. Test with a command such as this from the command line:
growlnotify -t programName -p normal -m 'message....'
Besides normal notifications, growlnotify can be useful in a script to alert the user to do something, with the 'wait' and 'sticky' flags it will wait till you click it to go away:
growlnotify -w -s -m 'tests have finished running; click this to run again'

Disable/Cancel Sleep Command on MacOSX

It seems to be impossible to completely disable the Sleep option in MacOSX so that a user cannot manually put the system to sleep.
Is there a way in Leopard (or even Snow Leopard) for AppleScript to catch the Sleep event and cancel it?
The answer to your problem is however a simple Apple-osx-Terminal command that prevents OSX to go idle:
$ pmset noidle
Preventing idle sleep (^C to exit)...
The only way to reset the no-sleeping is to issue a 'Control-C'.
See the man pages of pmset for more details.
This command has been deprecated in favor for
$ caffeinate -i
Which does roughly the same.
You can also run caffeinate for a pre-determined amount of time to prevent sleeping, say for 4 hours while you download something, and then run it in the background by adding & to it:
$ caffeinate -t 144000 &
You can't do this from user mode, because the power manager prevents it, so you'll need to use a kernel extension, such as InsomniaX.
Newer version(s) of OSX make it more possible to disable certain options according to https://apple.stackexchange.com/questions/35256/mac-os-x-lion-server-how-to-disable-sleep-restart-shutdown-options-for-users

Resources