Centos Init.d And Install Script


igordata

Recommended Posts

Hi!

 

I'm a big fan of bittorrent technology. I hope some day we will use it as direct urls in browser to navigate and load any content in real time.

 

Today I decided to make btsync a main tool that will transfer backups of my sites to my PC. I prefer CentOS as server OS. But it's usual repos (main+epel+remi) has no btsync package yet. May be because of beta status of that tool.

 

CentOS have very handy way to start and stop daemons, but you need init.d script. Shure, I've found some. While trying to make it all work, I occasionally created a kind of an installation script. And one uninstallation script too.

 

So, now I have three scripts for you to share. May be it will be useful for someone.

 

 

 

install.sh

This script will install btsync executable into the depths of CentOS.

You have to put this script and btsync binary in the same folder.

It do exactly the next things:

 

 

  • Creates user btsync if it doesn't exist
  • Creates folders:
    • /var/run/btsync - for a pid file
    • /var/lib/btsync - to store any files btsync need for work (mean only sync folder for now)
  • Creates files:
    • /usr/bin/btsync - executable
    • /etc/btsync.conf - config file
    • /etc/init.d/btsync - to control the daemon
    • /etc/sysconfig/bysync - to set /etc/init.d/btsync script vars (like user, config or pidfile) without editing it.

 

uninstall.sh

This script removes all above, except user and config file.

 

btsync.initd

To start btsync as daemon in a usual way - chkconfig or "service btsync start"

 

I'm not familliar with all that bash script writing. I hope someone could fix if I did something wrong.

Thanks!

 

Listing:

 

 

install.sh

#!/bin/sh# created by igordataprog=$(basename btsync)BINDIR=/usr/binCONFIG=/etc/$prog.confPROGDIR=/var/lib/$progPIDDIR=/var/run/btsync###################################################### Dont change anything below#####################################################BIN=$BINDIR/$progPIDFILE=$PIDDIR/$prog.pid#####################################################function createUser () {  user=$1  if id -u $user >/dev/null 2>&1; then    echo "User $user exists."  else    useradd -s /sbin/nologin -M -U $user || { echo "Can't create user $user."; exit 1; }    echo "User $user created."  fi}function createDir () {  DIR=$1  if [ -d $DIR ]; then    echo "Dir $DIR exists."  else    mkdir $DIR || { echo "Can't create directory $DIR."; exit 2; }    echo "Dir $DIR created."  fi}function chownchmod () {  DIR=$1  chown $prog:$prog $DIR || { echo "Can't change owner and group to $prog for path $DIR."; exit 3; }  chmod 0744 $DIR || { echo "Can't set permissions for path $DIR."; exit 3; }  echo "Owner, group and permissions are set for path $DIR.";}######################################################create user btsynccreateUser $prog#create folders#directory for btsync working filescreateDir $PROGDIRchownchmod $PROGDIR#btsync storage dircreateDir $PROGDIR/syncchownchmod $PROGDIR/sync#directory for btsync pid filecreateDir $PIDDIRchownchmod $PIDDIR#copy executablecp ./$prog $BIN || { echo "Can't copy $prog to $BIN."; exit 4; }chmod 0755 $BIN || { echo "Can't set permissions for executable $BIN."; exit 5; }echo "Executable copied to $BIN and permissons are set."cp ./$prog.initd /etc/init.d/$prog || { echo "Can't copy init.d script to /etc/init.d/$prog."; exit 6; }echo "Init.d script /etc/init.d/$prog created and permissions are set"touch /etc/sysconfig/$prog || { echo "Can't create /etc/sysconfig/$prog configuration file.";}chmod 0644 /etc/sysconfig/$prog || { echo "Can't set permissions for /etc/sysconfig/$prog configuration file.";}echo "Init.d script configuration vars now can be set in /etc/sysconfig/$prog file."if [ -f $CONFIG ]; then  echo "Config file $CONFIG exists."else  echo "{\"device_name\":\"CentOS.btsync\",\"listening_port\":0, // 0 - randomize port\"storage_path\":\"$PROGDIR/sync\",\"pid_file\":\"$PIDFILE\",\"use_upnp\":true,\"download_limit\":0,\"upload_limit\":0,/*  \"webui\" :  {    \"listen\" : \"0.0.0.0:8888\",    \"login\" : \"admin\",    \"password\" : \"pass\"  },*//*  \"shared_folders\" :  [    {      \"secret\" : \"VERYSPECIALSECRETPHRASEFORTHATDIR\", // use --generate-secret in command line to create new secret      \"dir\" : \"/path/to/dir\",      \"use_sync_trash\" : false, // enable to store files deleted on remote devices in .SyncArchive folder      \"known_hosts\" :      [        \"192.168.1.2:44444\"      ]    }  ],*/\"check_for_updates\":true}" > $CONFIG  echo "An empty config was created.Attention!You have to uncomment webui section and set user and password to access webui at http://localhost:8888 to be able create and share folders.Or uncomment and fill list of shared directories manualy."fi#####################################################echo "Now you can use \"service $prog (start|stop)\" commands.Thanks.Done.";

uninstall.sh

#!/bin/shprog=$(basename btsync)BINDIR=/usr/binCONFIG=/etc/$prog.confPROGDIR=/var/lib/$progPIDDIR=/var/run/btsync###################################################### Dont change anything below#####################################################BIN=$BINDIR/$progPIDFILE=$PIDDIR/$prog.pid#####################################################if [ -f $PIDFILE ]; then  killall $progfirm -f $PIDFILErm -f $BINrm -f /etc/init.d/$progrm -f /etc/sysconfig/$progrm -f -rf $PROGDIRrm -f -rf $PIDDIRecho "Done.User $prog and config file $CONFIG have to be removed manualy."

btsync.initd (put to /etc/init.d/ and rename to "btsync")

#!/bin/sh### BEGIN INIT INFO# Provides: btsync# Required-Start: $local_fs $remote_fs# Required-Stop: $local_fs $remote_fs# Should-Start: $network# Should-Stop: $network# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: service btsync (start|stop)# Description: Starts the btsync daemon.### END INIT INFO#############################USER="btsync"CONFIG=/etc/btsync.confPIDFILE=/var/run/btsync/btsync.pid#############################prog="btsync"retval=0if [ -f /etc/sysconfig/$prog ];then 	. /etc/sysconfig/$progfi# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0start() {  if [ -f $config ]; then    echo -n $"Starting $prog: "    daemon --user $USER $prog --config $CONFIG    echo  else    echo "Couldn't start $prog for $USER (no $config found)"  fi} stop() {  echo -n $"Stopping $prog: "  killproc -p $PIDFILE  echo}  case "$1" in start)start;;stop)stop;;restart|reload|force-reload)stopstart;;status)status $progretval=$?;;*)echo "Usage: /etc/init.d/btsync {start|stop|reload|force-reload|restart|status}"exit 1esac exit 0

btinstall.zip

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.