2

Every time I shutdown or restart my laptop (Ubuntu 14.04), I would like to run a script that checks, whether I pushed my newest code to my remote git repository. If I forgot it, then it opens a terminal, asks the user to enter a commit message and pushes the changes. I already got the script working.

Now I am looking for a way to make this script run automatically when I shutdown or reboot, but before the GUI exits.

My approach so far is with System V Init (yes, I know it's a bit dated):

I copy my init script with LSB header to /etc/init.d:

sudo cp ~/git_checker /etc/init.d/

, change permissions:

sudo chmod a+x /etc/init.d/git_checker

and configure execution scenarios:

sudo update-rc.d /etc/init.d/git_checker defaults

When I test this script with sudo service git_checker start, I get the error: "Failed to parse arguments: Cannot open display:"

Reading up on it, I found out, that init scripts should not be used to open terminals (like so: su user -c 'x-terminal-emulator -e /home/user/git_check.sh' ), because the X server is not guaranteed to be running when init scripts are executed.

So init scripts seem to be the wrong way. Is there another way? Maybe with upstart or systemd?

In the case of running the script at system startup, I can simply put it in startup applications. Does something similar exist, like shutdown applications?

Oscillon
  • 154
  • 1
  • 9
  • See http://askubuntu.com/questions/720380/how-can-i-run-a-local-command-to-run-a-script-on-just-before-log-out-of-a-un – Jacob Vlijm Mar 19 '16 at 17:24

1 Answers1

1

I've created a small monitoring script some time ago that will call an interrupt function once the script detects user's attempt at shutting down the computer. The small modification that it needs for your specific case is to cancel the shutdown action , run the script and then call shutdown.

#!/bin/bash

main()
{
  dbus-monitor --profile "interface='com.canonical.Unity.Session',type=signal,member=RebootRequested" | \
  while read -r line;
  do
#   echo $line
     grep -q '.*NameAcquired.*' <<< "$line"  && continue  #  Ignore that first line
    if [ -n "$line"  ];then
       interrupt 
    fi
  done
}

interrupt()
{ 
  # The first command will close the shutdown dialog
  qdbus com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.CancelAction
  # place call to your script bellow this comment
  zenity --info --text='Remember to push changes to git repo'
  # Uncomment line bellow for shutdown
  # qdbus com.canonical.Unity  /com/canonical/Unity/Session com.canonical.Unity.Session.Shutdown

}

main

This script of course must be added as part of startup applications or you can create manually .desktop file for it

NOTE: This script works only with GUI, so if user issues a command sudo shutdown -P now , it won't work. You would need to also monitor for shutdown command through another script using pgrep shutdown or integrate another funciton into the scrip.

For example, in my script up above , youd want to add this function

manual_shutdown_monitor()
{
  while true 
  do
  if pgrep shutdown > /dev/null
  then
      zenity --info --text="GOT MANUAL"
  fi
  sleep 0.25
  done
}

And then call that function in main() like this

manual_shutdown_monitor &
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Does the system suffer from any considerable slow down due to constantly checking for shutdown signals? I guess the answer is no? – Oscillon Mar 19 '16 at 18:14
  • @oscillion I've a script that uses pretty much this same structure and polls for running apt or dpkg. Ive not noticed any slowdown so far. Slowdown may occur in a function such as the one polling for manual shutdown with while loop. The 0.25 sleep delay is important there and helps avoiding the slowdown – Sergiy Kolodyazhnyy Mar 19 '16 at 18:19
  • While playing around with the script, I noticed that it interrupts the shutdown only once. How come? – Oscillon Mar 19 '16 at 18:53
  • @Oscillon The graphical shutdown can be interrupted multiple times. Was that manual command line shutdown you were trying to do ? – Sergiy Kolodyazhnyy Mar 19 '16 at 18:57
  • That is what I thought, too - but I am using the shutdotwn from the clickable menu. It only prevented the shutdown once. Tried it 3times and always the same outcome. – Oscillon Mar 19 '16 at 19:00
  • Anyways, your answer is amazing! Lucky me, that I did not ask about this 2 days ago ;) No admin privileges required and no init scripts need to be written! I would upvote it more, but I don't have enough reputation, yet. PS: Is there any way, something similar can be done under Windows? I know that this goes in a way different direction, but I am dealing with a multi platform environment. – Oscillon Mar 19 '16 at 19:02
  • @Oscillon Unfortunately i am not familiar with Windows environment in any way, so can't help in that department. The interrupt works perfectly fine for me multiple times. The only thing I can think zenity not exiting completely when you try for second shutdown attempt. That's the only possible thing. Try using notify-send 'TEST' instead of that zenity command – Sergiy Kolodyazhnyy Mar 19 '16 at 19:08