Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
up vote 1 down vote accepted

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 &
share|improve this answer
    
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 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 – Serg Mar 19 at 18:19
    
While playing around with the script, I noticed that it interrupts the shutdown only once. How come? – Oscillon Mar 19 at 18:53
    
@Oscillon The graphical shutdown can be interrupted multiple times. Was that manual command line shutdown you were trying to do ? – Serg Mar 19 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 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.