Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper init script #436

Open
severeski opened this issue Mar 1, 2015 · 1 comment
Open

Proper init script #436

severeski opened this issue Mar 1, 2015 · 1 comment

Comments

@severeski
Copy link

Ok guys,

First off vysheng I really love this this project so I decided to pitch in.

I wrote a proper init script for deamon mode telegram-cli (i hope;) )

There still are some quirks but I can tweak etc all I want but I decided to put it out here so I can get some propper feedback. Seemed more usefull to me.

The script is based for use on raspbian (raspberry pi debian wheezy).

First the files:

/etc/init.d/telegram-cli


#! /bin/sh
### BEGIN INIT INFO
# Provides:          telegram-cli
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Commandline interface for Telegram chat program
# Description:       Telegram-cli is a (unofficial) cli version of Telegram to chat from your console.
#                    This is an init script do make it a daemon.
#                    When used as daemon in conjuction  with LUA (scripting) you can use it to send your system
#                    commands to execute via other Telegram apps (PC - Phone - Web or other) while not
#                    logged in to the system.
#
#                    Note #1: This version of the init script is developed for raspbian (rapberry PI port of Debian Wheezy).
#
#                    Note #2: Change the ¨DAEMON=¨ var to match your installation path of telegram-cli
#
#                    See: https://github.com/vysheng/tg for more information.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Telegram-cli"
NAME=telegram-cli
DAEMON=/<my_path>/$NAME
PIDFILE=/var/run/$NAME.pid

. /lib/lsb/init-functions

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Carry out specific functions when asked to by the system
case "$1" in

    start)
        echo -n "Starting $DESC ... "
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec $DAEMON -- $OPTS || true
        echo "Done."
    ;;

    stop)
        echo -n "Stopping $DESC ... "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec $DAEMON || true
    echo "Done."

    ;;
    restart)
        echo -n "Restarting $DESC "
        start-stop-daemon --stop --retry 2 --pidfile $PIDFILE \
            --exec $DAEMON || true
        start-stop-daemon --start --background --make-pidfile $PIDFILE --pidfile $PIDFILE \
            --exec $DAEMON -- $OPTS || true
        echo "Done."
    ;;

    status)
        status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
    ;;

    *)
        N=/etc/init.d/$NAME
        echo "Usage $NAME: /etc/init.d/telegramd {start|stop|restart|status}"
        exit 1
    ;;

esac

/etc/defaults/telegram-cli

#
# Default setting voor telegram-cli sourced in /etc/init.d/telegram-cli.
#
# For the different options execute: 'telegram-cli --help'.
#
# Note: use the -R -C option if you want to write to log file. Otherwise it will just print garbage.
#
#
OPTS="-W -U pi -G pi -s <path_to_lua_script>/test.lua -P 1234 -d -L /var/log/telegram-cli.log -vvvRC"

:Some notes to get it working correctly (I wrote comments on it in the files themself as best as I could but to be sure):

The idea is the following:

/etc/telegram-cli/server.pub holds your server.pub key for which the program searches on startup. Available in source dir after gt checkout.

/etc/default/telegram-cli holds all the config options. The included sample code is kinda example enough by it's self ;)

/etc/init.d/telegram.cli (the actual init script) Important is to change the "DAEMON=" to the directory you have you're install of the telegram-cli program.

Work in progress is /var/log/telegram-cli.log file. For now I created it by hand an gave it world read write permission till i work out how to properly create one.

  1. Most important is to change the "DAEMON=" parameter in /etc/init.d/telegram-cli file to your installation of telegram-cli program.

  2. It is forced run with username 'pi' for now. This can be any other user kinda depends on under which user you registered telegram-cli under..

  3. To make it run on boot when you are registered do:

update-rc.d -f telegram-cli defaults
  1. it create's a TCP port 1234 to give it commands via netcat or telnet via localhost or other. See: https://github.com/vysheng/tg/wiki/Running-as-daemon scroll to end of the post for an example.

  2. You need to register a different phone number then you own to make LUA scripting work!!! That drove nuts while fucking arround with it. I just registered my parents mobile number cause they do jack shit with their mobile. Let alone telegram ;P

  3. If you want to run it as daemon you need a real homedir/login and run it once interactively to register your number and write the proper ~/.telegram-cli files. You can also copy those files from another user it seems. Up to you :)
    Do not ry to backspace or anything it will mostly give you crap. just (country code) + (number) example for NL users: +310612345678 (you can ommit the 0 btw 0 ;) )

/<your install dir>/telegram-cli -W -k /<your dir>/tg-server.pub

This can take some time the first time, so be patient. Register you phone and type the code you get via sms/or phone call.

Then type

safe_quit

And ya are good to go ...
For me this works like a charm.

I would really like to have some feedback on how to make certain things more generic and/or trouble you run into or anything other.

Cheers!

@poktor
Copy link

poktor commented Dec 16, 2016

When installing on centos 6 i noticed i don't have the script start-stop-daemon. Also /lib/init/vars.sh was missing. So, i modified the init.d-script a bit Here it is:
First, the defaults-script in /etc/default:

OPTS="-W -dL /var/log/telegram-cli.log -vvvvRC -k /etc/telegram-cli/server.pub -P 1234"

And here is the init.d script:

#! /bin/sh ### BEGIN INIT INFO
# Provides: telegram-daemon # Required-Start:
# Required-Stop: # Default-Start: S 2 3 4 5
# Default-Stop: 0 6 # Short-Description: telegram daemon
# Description: telegram daemon ### END INIT INFO
# Author: fizzzel # actions
case "$1" in
start)
echo "Telegram daemon is starting..."
su telegramd -c '/usr/bin/telegram-cli -vvvvRC -k /etc/telegram-cli/server.pub -W -dL /var/log/telegram-cli.log -P 1234 &'
echo "...done"
;;
stop)
echo "stopping telegram daemon..."
sudo pkill -9 telegram-cli
echo "..done"
;;
restart)
echo "stopping telegram daemon..."
sudo pkill -9 telegram-cli
echo "..done -- restarting..."
su telegramd -c '/usr/bin/telegram-cli -vvvvRC -k /etc/telegram-cli/server.pub -W -dL /var/log/telegram-cli.log -P 1234 &'
echo "...done"
;;
esac

exit 0

`

This works, i have now 2 servers running this 24/7.
To make this work, first make sure that everything is in place: telegram-cli in /usr/bin, server.pub in /etc/telegram-cli and OPTS-line mentioned earlier in /etc/default/lelegram-cli. Then create a user telegramd. then do a su - telegramd and run telegram-cli manually. Fill in phone number and receive SMS-code. Fill it in. As said, you can also copy the /.telegram-cli folder from somewhere else, provided you know the number it is connected to. Send a msg to yourself to test. Then type quit. Then you can start it as a daemon using service telegram-cli start.

Good luck.

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

No branches or pull requests

2 participants