Rich Tier
Rich Tier

Reputation: 9441

bash script to open chrome then turn computer off when chrome is closed

I would like to write the a startup script in bash to make an ubuntu box more grandpa-friendly.

The script should:

open chrome wait until chrome is closed turn computer off

so far I have

#!/bin/bash
if [ -z "$1" ]; then
    address="http:\\www.google.co.uk"
else
    address=$1
fi

echo starting the internet
google-chrome $address

while [ 1=1 ];
do
   grep_resp=$(ps aux | grep chrome)
   if [ -z "$grep_resp" ]; then
       echo turning computer off 
   else
       echo chrome still running
   fi
   sleep 5
done 

but the grep for "chrome" is in the process list

Any aid?

Upvotes: 3

Views: 3010

Answers (3)

larsks
larsks

Reputation: 311635

You're getting that error because $grep_resp in this...

if [ -z $grep_resp ]; then

...probably expands into a string containing whitespace. If you put quotes around it:

  if [ -z "$grep_resp" ]; then

It will do what you expect. However, I think all of that may be unnecessary. Chrome doesn't automatically background or anything when it runs, so you should be able to do this:

google-chrome $address
echo turning computer off
...

Upvotes: 3

Charles Ma
Charles Ma

Reputation: 49171

Line 14 should be

if [ -z "$grep_resp" ]; then

It needs to be in quote because the string has spaces in it.

Upvotes: 1

bitmask
bitmask

Reputation: 34626

How about:

#!/bin/bash

user=gramps

sudo -u "$user" -- chrome "$1"; shutdown -P now

where the script is run as root.

Upvotes: 1

Related Questions