3

I want to create a daemon of Telegram-cli (https://github.com/vysheng/tg). I have created the file /etc/systemd/system/telegram.service with the following contents:

[Service]
Type=simple
KillMode=process
ExecStart=/usr/bin/telegram-daemon

[Install]
WantedBy=default.target

And a file /usr/bin/telegram-daemon with the following contents:

#!/bin/bash
rm -rf /var/run/telegram.sock
cd /root/tg
bin/telegram-cli  -k tg-server.pub -W -s action.lua  -S /var/run/telegram.sock > /dev/null 2>&1 &
exit

I can perfectly start this service using systemctl start telegram, I can enable it using systemctl enable telegram:

 ln -s '/etc/systemd/system/telegram.service' '/etc/systemd/system/default.target.wants/telegram.service'

When I manually start the server and check if telegram is running using ps aux | grep telegram it is running:

root      2506  0.0  2.2 359208 22632 ?        S    apr24   1:11 bin/telegram-cli -k tg-server.pub -W -s action.lua -S /var/run/telegram.sock
root      8177  0.0  0.0 112660   964 pts/0    R+   12:11   0:00 grep --color=auto telegram

However when I reboot, the cli isn't started. The status of the service (systemctl status -l telegram) is:

telegram.service
   Loaded: loaded (/etc/systemd/system/telegram.service; enabled)
   Active: inactive (dead) since zo 2015-04-26 12:13:14 UTC; 48s ago
  Process: 393 ExecStart=/usr/bin/telegram-daemon (code=exited, status=0/SUCCESS)
 Main PID: 393 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/telegram.service

apr 26 12:13:14 vultr.guest systemd[1]: Starting telegram.service...
apr 26 12:13:14 vultr.guest systemd[1]: Started telegram.service.

How can I solve this? Note that I also tried to change the type to forking. And I tried to directly run the telegram command from the service. In the latest situatuon systemctl start telegram keeps running for ever.

Thanks in advance!

P.s. I'm running Centos 7

EDIT: note that starting the service at @reboot via crontab has the same result.

2

I managed to solve this. Here is the /etc/systemd/system/telegram.service file. Note that this is the only file you'll need.

[Unit]
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
ExecStartPre=/bin/rm -f /var/run/telegram.sock
ExecStart=/root/tg/bin/telegram-cli  -k /root/tg/tg-server.pub -W -s /root/tg/action.lua  -S /var/run/telegram.sock > /var/log/telegram.log &
KillMode=process

ExecStop=/bin/rm -rf /var/run/telegram.sock

[Install]
WantedBy=multi-user.target

You'll have to install and compile telegram under /root/tg. Now you can start it with systemctl start telegram, stop it with systemctl stop telegram and enable it (so it will run at boot) via systemctl enable telegram

Now you can start sending messages via echo "msg $username $msg" | socat - UNIX-CONNECT:/var/run/telegram.sock. Replace $username and $msg with your values. This can be simplified by creating a bash script (place it under /usr/bin/telegram-msg and do a chmod +x /usr/bin/telegram-msg

#!/bin/bash
echo "msg $1 $2" | socat - UNIX-CONNECT:/var/run/telegram.sock

Now you can send telegram messages via telegram-msg $username $msg.

|improve this answer|||||

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.