Script to shut down the computer when closing a particular program

Asked

Viewed 1,140 times

3

How do I create a script so that when the rdesktop window is closed the computer will be shut down?

2 answers

1


André Cabral

Would close a program or window?

Considering some kind of program:

Create a file with the name you want, the examples below refer to Ubuntu:

touch desliga_automatico

Put the permission to execute:

sudo chmod +x desliga_automatico

Put the code down:

#!/bin/sh
PROGRAMA='rdesktop' #nome do programa que deseja monitorar

while [ TRUE ] 
do
    if ps ax | grep -v grep | grep $PROGRAMA > /dev/null
    then
        #echo "$PROGRAMA está rodando"
    else
        #echo "$PROGRAMA não está rodando"
        sudo reboot 
    fi
    # colocamos o sleep para o processador ter tempo para realizar outras tarefas
    # você pode colocar o tempo que achar melhor (em segundos)
    sleep 120
done

In case it is a window and not a program:

#!/bin/sh
PROGRAMA='rdesktop'

while [ TRUE ]
do

    if xlsclients | grep -v grep | grep $PROGRAMA > /dev/null
    then
        #echo "$PROGRAMA está rodando"
    else
        echo "$PROGRAMA não está rodando"
        sudo reboot
    fi
    # colocamos o sleep para o processador ter tempo para realizar outras tarefas
    # você pode colocar o tempo que achar melhor (em segundos)
    sleep 120
done

If this meets your need you can put it on:

sudo mv desliga_automatico /etc/init.d/ 

for it to start every time the computer starts

I hope I’ve helped.

  • Edit the answer. It’s fine, but put in /etc/init.d/ not enough to run the application on boot. In addition to giving permission, you have to update the runlevels And to rotate in the boot tb has to change the application header by citing the runlevels, etc..

-1

#!/bin/sh
PROGRAMA='rdesktop'

while [ TRUE ]
do

    if xlsclients | grep -v grep | grep $PROGRAMA > /dev/null
    then
        echo "$PROGRAMA está rodando"
    else
        echo "$PROGRAMA não está rodando"
        sudo reboot
    fi
    # colocamos o sleep para o processador ter tempo para realizar outras tarefas
    # você pode colocar o tempo que achar melhor (em segundos)
    sleep 120
done

PS: There was a line commented before Else so the error code, now it’s right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.