Skip to content

User level daemon on Arch linux with systemd

jandrioli edited this page Mar 19, 2015 · 4 revisions

I'm writing this because I saw lots of people struggling to get telegram-cli working as a server. Since systemd 206 you can have user-services that autostart with the machine. Awesome!

My 2cents here: telegram-cli should not be run as a daemon. Telegram-cli is a client which connects to servers on the internet, and making it a service on linux seems, to me, clearly outside of its original design. Just imagine that you have several users logged on your machine in parallel and all of them can use your telegram-cli....

Having said that, I understand that many of us want scripts to talk to telegram-cli. Conclusion is, we shouldn't be angry or frustrated, after all telegram-cli is not conceived/built for our use. We are trying to hack it into something new. And here's how I did it:

  1. did all this as normal user, and not root

  2. compiled with: git clone --recursive {url}; ./configure; make; (don't try to make install)

  3. registered my machine/phone: ~/tg/bin/telegram-cli -k ~/tg/server.pub (had to wait quite much for startup)

  4. tested a few commands like contact_list etc. and killed with CTRL+C

  5. the instructions on how to run as daemon do not really apply to Arch. But that's ok, vysheng doesn't need to do everything for us. Since my telegram-cli will run as a user-level service, I don't need that. From those instructions we have the golden key: attach telegram-cli to a port and use netcat to talk with it. Now, create a systemd-user-level-service: ~/.config/systemd/user/telegram.service and put this content inside of that file.

[Unit]
Description=Telegram-cli systemd service for username1

[Service]
ExecStart=/home/username1/tg/bin/telegram-cli -d -vvvv -P 2391 -L /var/log/telegram-daemon/telegram-cli.log

[Install]
WantedBy=default.target
  1. And now you can enable this service and start it from systemd, BUT REMEMBER to run as user and not root!
username1@machine1:~$ systemctl --user enable telegram
Created symlink from /home/username1/.config/systemd/user/default.target.wants/telegram.service to /home/username1/.config/systemd/user/telegram.service.

username1@machine1:~$ systemctl --user status telegram
 telegram.service - Telegram-cli systemd service for username1
   Loaded: loaded (/home/username1/.config/systemd/user/telegram.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
username1@machine1:~$ systemctl --user start telegram

username1@machine1:~$ systemctl --user status telegram
 telegram.service - Telegram-cli systemd service for username1
   Loaded: loaded (/home/username1/.config/systemd/user/telegram.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2015-03-19 11:25:25 EDT; 4s ago
 Main PID: 25694 (telegram-cli)
   CGroup: /user.slice/user-1000.slice/[email protected]/telegram.service
           `-25694 /home/username1/tg/bin/telegram-cli -d -vvvv -P 2391 -L /var/log/telegram-daemon/telegram-cli.log

Congratulations! You can now simply use netcat to pipe commands to telegram-cli, like in the example from vysheng echo "msg Girlfriend 'Lets go out?' " | nc localhost 2391 -c.
Enjoy!

#Important note I am not responsible to problems you might face with your privacy. Mind you, this solution opens a port which not exclusive to you. Everyone and anyone can send messages on your behalf, see your contacts, open your attachments, etc... be careful what you do with this guide.

Clone this wiki locally