Update README for archival
[reddit.git] / scripts / saferun.sh
blobc4f7121c2a659edac4cfc90911395722dd12e593
1 #!/bin/bash
2 # The contents of this file are subject to the Common Public Attribution
3 # License Version 1.0. (the "License"); you may not use this file except in
4 # compliance with the License. You may obtain a copy of the License at
5 # http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
6 # License Version 1.1, but Sections 14 and 15 have been added to cover use of
7 # software over a computer network and provide for limited attribution for the
8 # Original Developer. In addition, Exhibit A has been modified to be consistent
9 # with Exhibit B.
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13 # the specific language governing rights and limitations under the License.
15 # The Original Code is reddit.
17 # The Original Developer is the Initial Developer. The Initial Developer of
18 # the Original Code is reddit Inc.
20 # All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
21 # Inc. All Rights Reserved.
22 ###############################################################################
24 if [ $# -lt 2 ]; then
25 echo "usage: $0 [pidfile] [command]" 1>&2
26 exit 1
29 PIDFILE=$1
30 shift 1
31 COMMAND=$1
32 shift 1
34 #check pid file for process
35 if [ -a $PIDFILE ]; then
36 c=$(ps -p $(cat $PIDFILE) | wc -l)
37 if [ $c -eq 2 ]; then
38 echo 'already running' 1>&2
39 ls -l $PIDFILE 1>&2
40 exit 1
44 #dump pid
45 echo "$$" > $PIDFILE
47 #run command
48 $COMMAND "$@"
50 #remove pid file
51 rm $PIDFILE