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 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 &