Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

start at boot time #9

Open
fometeo opened this issue Apr 16, 2020 · 3 comments
Open

start at boot time #9

fometeo opened this issue Apr 16, 2020 · 3 comments

Comments

@fometeo
Copy link

fometeo commented Apr 16, 2020

any suggestion for start dockssh at boot time over CentOS 7 ? it should be basic however I am missing something; dockssh is an excellent utility, thanks.

@alash3al
Copy link
Owner

you can use systemd, init.rc, pm2 or even docker!

personally I'm using pm2 as it is cross platform and enabling pm2 itself as a boot-time-service using pm2 startup,

here is pm2.

note, the above are just my suggestions, you can use any based on your needs

@fometeo
Copy link
Author

fometeo commented Apr 21, 2020

Hi. Thank you for the hints about pm2, quite interesting :-)

My problem at CentOS 7.5 was the place dockssh executable was.
Got this error: "open .ssh/id_rsa: no such file or directory" and
"Unregistered Authentication Agent for unix-process:1537:103982"
When at /root it worked. The init.d script was build based on this
https://github.com/wyhasany/sysvinit-service-generator service.sh
named dockssh.sh followed by these commands:

vi /etc/init.d/dockssh.sh
chmod +x /etc/init.d/dockssh.sh
chkconfig --level 345 dockssh.sh on

so it was registered, and worked on boot also

chkconfig --list
dockssh.sh      0:off   1:off   2:off   3:on    4:on    5:on    6:off

just to save time for an eventual reader this was the init.d dockssh.sh used

#!/bin/sh
### BEGIN INIT INFO
# Provides:          DOCKSSH 
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Description:       SSH INTO CONTAINERS
### END INIT INFO

SCRIPT="/root/dockssh"
RUNAS=root

PIDFILE=/var/run/dockssh.sh.pid
LOGFILE=/var/log/dockssh.sh.log

start() {
  if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
 # Try with this command line instead of above if not workable
 # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
 
  sleep 2
  PID=$(cat $PIDFILE)
    if pgrep -u $RUNAS -f $NAME > /dev/null
    then
      echo "$NAME is now running, the PID is $PID"
    else
      echo ''
      echo "Error! Could not start $NAME!"
    fi
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

status() {
    printf "%-50s" "Checking dockssh ..."
    if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
        PID=$(cat $PIDFILE)
            if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
                printf "%s\n" "The process appears to be dead but pidfile still exists"
            else    
                echo "Running, the PID is $PID"
            fi
    else
        printf "%s\n" "Service not running"
    fi
}



case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart}"
esac

@fometeo
Copy link
Author

fometeo commented Apr 22, 2020

personally I'm using pm2 as it is cross platform and enabling pm2 itself as a boot-time-service using pm2 startup,

after installing yarn and pm2

# yarn global add pm2
yarn global v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.1.3: The platform "linux" is incompatible with this module.
info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "pm2@4.3.1" with binaries:
      - pm2
      - pm2-dev
      - pm2-docker
      - pm2-runtime
Done in 5.80s.

removed the start from init.d

chkconfig --level 345 dockssh.sh off

rebooted, and applied pm2 start of dockssh - it worked
however dockssh does not start on a system restart

pm2 start /root/dockssh
pm2 startup

can it be done as above, just pm2 to /root/dockssh ?
I guess the fsevents error does not matter.
Anyway init.d method works, just interested about pm2.


update - solved

pm2 unstartup
pm2 startup centos -u root --hp /root
pm2 start /root/dockssh
pm2 save

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants