1

I have a RaspberryPi B+ which I connected to an USB HDD and a USB Wifi adapter. Both works great. Also, I have running a webserver and a seafile server.

Now, I want to set up a task (scheduled at 10pm) which shuts down the webserver and the seafile server by executing the corresponding command like ./seafile.sh stop etc. After that I want the USB HDD to be turned of as well as the USB Wifi.

There should be another task (scheduled at 7am) which does the wake up. It activates the USB HDD and the USB wifi and starts the services I need like ./seafile.sh start etc.

How can I do these task?

2

You first have to create a script.

Create a script file by typing:

sudo nano /etc/init.d/seafile

then copy past these lines :

#! /bin/sh
# /etc/init.d/seafile
#

# Some things that run always
touch /var/lock/seafile

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script seafile "
    # PUT HERE YOUR START SCRIPT
    ;;
  stop)
    echo "Stopping script seafile"
    # PUT HERE YOUR STOP SCRIPT
    ;;
  *)
    echo "Usage: /etc/init.d/seafile {start|stop}"
    exit 1
    ;;
esac

exit 0

Save and quit, and then make this file executable by typing :

sudo chmod +x /etc/init.d/seafile

Then you will have to put the right thing to start and stop your script (after # PUT HERE YOUR STOP SCRIPT and # PUT HERE YOUR START SCRIPT).

I dont know which webserver you are using, but if it's apache, you might put /etc/init.d/apache2 start to start the webserver, and /etc/init.d/apache2 stop to stop it.

Then, for your usb, you might mount and unmout them, for instance use mount /dev/sda, and umount /dev/sda (change path accordingly).

Your file may look like this :

#! /bin/sh
# /etc/init.d/seafile
#

# Some things that run always
touch /var/lock/seafile

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script seafile "
    # PUT HERE YOUR START SCRIPT
    /etc/init.d/apache2 start
    mount /dev/sda
    ;;
  stop)
    echo "Stopping script seafile"
    # PUT HERE YOUR STOP SCRIPT
    /etc/init.d/apache2 stop
    umount /dev/sda
    ;;
  *)
    echo "Usage: /etc/init.d/seafile {start|stop}"
    exit 1
    ;;
esac

exit 0

As crontab woks as a per-user basis,go to root by typing :

sudo -i

then start crontab by typing

crontab -e

Then add these lines to execute your script everyday at 7am and 10pm:

0 7 * * *  /etc/init.d/seafile start
0 22 * * *  /etc/init.d/seafile stop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.