SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-04-28 06:55:01

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

simple script to restart any process

#!/bin/sh
# restart any process
#
# usage: bl-restart [decimal] command arg1 arg2...
# The process matching the exact command line will be restarted after 1s,
# or by other number if passed before command.

help='usage: bl-restart [decimal] command arg1 arg2...
bl-restart -h|--help

Kills process matching full command line
and restarts after 1 sec, or "decimal" seconds if supplied.
(Floating-point is supported.)'

[ "$1" = '-h' ] || [ "$1" = '--help' ] && { echo "$help"; exit 0;}

delay=1

#type "$1" >/dev/null 2>&1 || {
command -v "$1" >/dev/null 2>&1 || {
    case "$1" in
    *[!0-9.]*)
        echo "$1 is not a command and not an integer.

$help"
        exit 1
        ;;
    *)
        delay=$1
        shift
        ;;
    esac
}

pkill -fx "$*" && sleep $delay
(setsid "$@" & )

exit 0

Last edited by johnraff (2015-04-30 09:15:57)

Offline

Help fund CrunchBang, donate to the project!

#2 2015-04-28 10:52:49

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2012-03-22
Posts: 1,026
Website

Re: simple script to restart any process

$ bl-restart [-w int] command arg1 arg2...

Offline

#3 2015-04-28 12:32:08

cpoakes
#! CrunchBanger
From: Tucson, Arizona
Registered: 2012-05-19
Posts: 202

Re: simple script to restart any process

@johnraff: I like it.  I prefer generalized and reusable solutions.

If you are going to provide command validation, I think it should be consistent and print an error whether the command appears as parameter $1 or $2. I don't like the use of "type".  While it is instrinsic and faster than "which", it also returns true for shell keywords and builtin commands.  It is possible to validate a command that is shell intrinsic but not a real executable and fail in the setsid fork. Or you could just forgo command validation altogether and accept the standard handling for a failed setsid.

Reworked:

#!/bin/sh
# restart any process
#
# usage: bl-restart [int] command arg1 arg2...
# The process matching the exact command line will be restarted after 1s,
# or by other number if passed before command.

help='usage: bl-restart [integer] command arg1 arg2...
bl-restart -h|--help

Kills process matching full command line
and restarts after 1 sec, or "integer" seconds if supplied.'

[ "$1" = '-h' ] || [ "$1" = '--help' ] && { echo "$help"; exit 0;}

case "$1" in
*[!0-9]*)
    delay=1
    ;;
*)
    delay=$1
    shift
    ;;
esac
which "$1" >/dev/null || { printf "$1 is not a command.\n\n$help\n"; exit 1; }

pkill -fx "$*" && sleep $delay
(setsid "$@" & )

# Unnecessary, sub-shell supplies 0 exit value whether command executes or not
#exit 0

Also, GNU sleep will accept floating point values.  You could add a "." to the digit pattern if you think there is any call for partial second wait.  I could think of a use for 0.1 or 0.5.  And "sleep 0" is valid even if unnecessary...

@iMBeCil: The "-w" suggestion makes sense.  Or always require (and always validate) an integer value.


programming and administering unix since 1976 (BSD, System III, Xenix, System V, Linux)

Offline

#4 2015-04-29 00:24:21

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: simple script to restart any process

Offline

#5 2015-04-29 05:20:13

cpoakes
#! CrunchBanger
From: Tucson, Arizona
Registered: 2012-05-19
Posts: 202

Re: simple script to restart any process

case `type $1` in
*alias*)
   ...
*function*)
   ...
*builtin*)
   ...
*)
   ...
esac

programming and administering unix since 1976 (BSD, System III, Xenix, System V, Linux)

Offline

#6 2015-04-30 01:37:25

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: simple script to restart any process

Offline

#7 2015-04-30 08:16:37

cpoakes
#! CrunchBanger
From: Tucson, Arizona
Registered: 2012-05-19
Posts: 202

Re: simple script to restart any process

^I think it remains accurate to consider differing outputs/return values for identically named utilities to be a issues of portability.  Addressing such differences in utilities and shell implementations was the essence of portability long before the POSIX definition was created.


programming and administering unix since 1976 (BSD, System III, Xenix, System V, Linux)

Offline

#8 2015-04-30 09:10:03

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: simple script to restart any process

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo