2

I'm trying to start a service over ssh in a bash script that I just call from the command line. I can do the following commands: (I have ssh keys in place so I don't need to put a p/w in)

ssh -t -t user@server 'sudo /sbin/service test stop' -- Works fine, stops the service

ssh -t -t user@server 'sudo /sbin/service test status' -- Works fine, status's the service

ssh -t -t user@server 'sudo /sbin/service test start' -- Doesn't start the service ???

This is with a custom init.d script running on centos 6.7

Does anyone have any ideas?

or see what I might be missing?

Thanks!

EDIT: Here is the init.d script:

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          test
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description:
### END INIT INFO
#

###source function library
source /etc/init.d/functions

APPNAME="test"
APPDIR="/tmp"
CONFIGDIR="/tmp/config"
LOCKFILE=/var/lock/subsys/$APPNAME
PIDFILE="/var/run/$APPNAME.pid"

###Declare variables for test
CONFIG="test.config"

start() {
    echo -n "Starting $APPNAME: "
    source "${CONFIGDIR}/source.txt"
    daemon --pidfile="$PIDFILE" "/tmp/${APPNAME} ${CONFIGDIR}/${CONFIG} >> /tmp/console.log 2>&1 &"
    RETVAL=$?
    [ $RETVAL -eq 0 ] && {
            touch $LOCKFILE
            pidof $APPNAME > $PIDFILE
    }
    return $RETVAL
}

stop() {
    echo -n $"Stopping $APPNAME:"
    killproc -p "$PIDFILE" $APPNAME
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $LOCKFILE $PIDFILE
    return $RETVAL
}

case "$1" in
    start)
            start
            ;;
    stop)
            stop
            ;;
    status)
            status -p "$PIDFILE" "$APPNAME"
            ;;
    restart)
            stop
            start
            ;;
    *)
            echo "Usage $prg {start|stop|status|restart}"
            exit 1
            ;;
esac
exit $RETVAL
georges
  • 175
  • 4
  • 17
  • Could you include the custom `init.d` script? – StephenG Jul 22 '16 at 17:33
  • What is "custom init.d"? – Jakuje Jul 22 '16 at 17:34
  • You might like to pass `-n` to ssh, to redirect `stdin` to `/dev/null`. It could be that the remote end is reading it, and getting stuck. And I suspect you don't want `-t`? – gilez Jul 22 '16 at 17:51
  • if i don't use the -t -t i get "sudo: sorry, you must have a tty to run sudo " i added the -n, same problem – georges Jul 22 '16 at 17:59
  • @georges, I'm pretty sure then that sudo is the problem then. It wants a tty to prompt for a password if needed. What if you do something like `'sudo /sbin/service test start /dev/null 2>&1'` ? That should force all file handles to detach from the terminal. Or close them with `>&-` etc... – gilez Jul 22 '16 at 18:23
  • @georges, see this: http://unix.stackexchange.com/questions/122616/why-do-i-need-a-tty-to-run-sudo-if-i-can-sudo-without-a-password – gilez Jul 22 '16 at 18:25
  • @gilez I ran this: ssh -n user@server 'sudo /sbin/service test start /dev/null 2>&1' &>> /tmp/test.crumbs, but the service still didn't start – georges Jul 22 '16 at 19:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118058/discussion-between-georges-and-gilez). – georges Jul 22 '16 at 19:35

2 Answers2

1

I found a work around:

ssh -t -t user@server 'sudo bash -s' < '/tmp/start_test.sh' 

and then on the server:

/tmp/start_test.sh

#!/bin/bash

sudo service test start >> /tmp/start.log

exit 0
georges
  • 175
  • 4
  • 17
0

I tested this one successfully:

echo "sudo service test start && exit" | python -c 'import pty, sys; pty.spawn(sys.argv[1:])' ssh user@server
Brice Ruppen
  • 81
  • 1
  • 6