1

I'm new to shell scripting, i want the command to be in running always. My .sh file - startscrapy.sh

#!/bin/bash 
echo "Scrapyd is  started now"
scrapyd

i have changed the permission also chmod +x etc/init.d/startscrapy.sh

I have placed this file in etc/init.d but it is not working. My understanding as of now " the location etc/init.d is to run the .sh files whenever the server or system boots up but i want my .sh file to be running state always.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
Vimal Annamalai
  • 139
  • 1
  • 2
  • 12

3 Answers3

0

Take a look at this init.d template and change your one accordingly. Then you need to register the startup script with your initialisation daemon. Under Ubuntu that would be update-rc.d NAMEofDAEMON default

Community
  • 1
  • 1
zabeltech
  • 963
  • 11
  • 27
0

You want to create a daemon. There are some tutorials on internet to do this , i took this one for you. On the final part, you might use a different way to register the script, this one is for ubuntu.

you need to put the following into a file of the name of your choice (i will take "startscrapy.sh" for now) (you can modify it, obviously, according to your needs)

#!/bin/sh -e

DAEMON="scrapyd" #Command to run
daemon_OPT=""  #arguments for your program
DAEMONUSER="user" #Program user
daemon_NAME="scarpyd" #Program name (need to be identical to the executable).

PATH="/sbin:/bin:/usr/sbin:/usr/bin" #don't touch

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

d_start () {
        log_daemon_msg "Starting system $daemon_NAME Daemon"
    start-stop-daemon --background --name $daemon_NAME --start --quiet --chuid $DAEMONUSER --exec $DAEMON -- $daemon_OPT
        log_end_msg $?
}

d_stop () {
        log_daemon_msg "Stopping system $daemon_NAME Daemon"
        start-stop-daemon --name $daemon_NAME --stop --retry 5 --quiet --name $daemon_NAME
    log_end_msg $?
}

case "$1" in

        start|stop)
                d_${1}
                ;;

        restart|reload|force-reload)
                        d_stop
                        d_start
                ;;

        force-stop)
               d_stop
                killall -q $daemon_NAME || true #replace with an apropriate killing method
                sleep 2
                killall -q -9 $daemon_NAME || true #replace with an apropriate killing method
                ;;

        status)
                status_of_proc "$daemon_NAME" "$DAEMON" "system-wide $daemon_NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

Then run as root :

chmod +x etc/init.d/startscrapy.sh
chmod 0755 /etc/init.d/startscrapy.sh (modify by your script location)
systemctl daemon-reload
update-rc.d startscrapy.sh defaults

To remove the daemon, run as root :

update-rc.d -f startscrapy.sh remove
0

Using crontab you can easily auto start any scripts in ubuntu.
Please do the following steps,

  1. Run the command crontab -e so that you can edit the crontab.
  2. Now add the following line to the crontab editor @reboot sudo <script> in your case it should be @reboot sudo scrapyd.
  3. Now reboot your system, then you will find scrapyd running.

Hope it Helps.

Karthikeyan KR
  • 1,134
  • 1
  • 17
  • 38