Welcome to the Linux Foundation Forum!

Please help create systemd unit

Hello!

I'm using Ubuntu 18.04.4 LTS
I want to create systemd unit which will run script on disk mount.

Here is what I'm trying to do:

  1. I've created a script:

!/bin/bash

/usr/bin/yandex-disk-indicator
and made it executible. It works fine when I run it through terminal. It starts and runs till I close term.
I named it yaIndStart and placed it to /usr/local/bin
2. I've created systemd service named yaIndStart.service and placed it into /etc/systemd/system
3. Enabled service: sudo systemctl enable yaIndStart.service
4. Started service: sudo systemctl start yaIndStart.service but there is an error: "Job for yaIndStart.service failed because the control process exited with error code.
See "systemctl status yaIndStart.service" and "journalctl -xe" for details."

Some outputs:
sudo systemctl start yaIndStart.service output: "● yaIndStart.service - starts Yandex Disk Indicator when /media/kamensky/Data is
Loaded: loaded (/etc/systemd/system/yaIndStart.service; enabled; vendor prese
Active: failed (Result: exit-code) since Fri 2020-04-24 10:16:01 MSK; 24s ago
Process: 13280 ExecStart=/usr/local/bin/yaIndStart (code=exited, status=139)
Main PID: 13280 (code=exited, status=139)

апр 24 10:15:58 kamenskyhome systemd[1]: Starting starts Yandex Disk Indicator w
апр 24 10:15:58 kamenskyhome yaIndStart[13280]: Unable to init server: Could not
апр 24 10:15:58 kamenskyhome yaIndStart[13280]: Unable to init server: Could not
апр 24 10:16:01 kamenskyhome yaIndStart[13280]: Ошибка сегментирования (core dum
апр 24 10:16:01 kamenskyhome systemd[1]: yaIndStart.service: Main process exited
апр 24 10:16:01 kamenskyhome systemd[1]: yaIndStart.service: Failed with result
апр 24 10:16:01 kamenskyhome systemd[1]: Failed to start starts Yandex Disk Indi
lines 1-13/13 (END)...skipping...
● yaIndStart.service - starts Yandex Disk Indicator when /media/kamensky/Data is mounted
Loaded: loaded (/etc/systemd/system/yaIndStart.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2020-04-24 10:16:01 MSK; 24s ago
Process: 13280 ExecStart=/usr/local/bin/yaIndStart (code=exited, status=139)
Main PID: 13280 (code=exited, status=139)

апр 24 10:15:58 kamenskyhome systemd[1]: Starting starts Yandex Disk Indicator when /media/kamensky/Data is mounted...
апр 24 10:15:58 kamenskyhome yaIndStart[13280]: Unable to init server: Could not connect: Connection refused
апр 24 10:15:58 kamenskyhome yaIndStart[13280]: Unable to init server: Could not connect: Connection refused
апр 24 10:16:01 kamenskyhome yaIndStart[13280]: Ошибка сегментирования (core dumped)
апр 24 10:16:01 kamenskyhome systemd[1]: yaIndStart.service: Main process exited, code=exited, status=139/n/a
апр 24 10:16:01 kamenskyhome systemd[1]: yaIndStart.service: Failed with result 'exit-code'.
апр 24 10:16:01 kamenskyhome systemd[1]: Failed to start starts Yandex Disk Indicator when /media/kamensky/Data is mounted."

and journalctl -xe output:

"апр 24 10:15:59 kamenskyhome systemd[1]: Starting Process error reports when automatic reporting is enabled...
-- Subject: Начинается запуск юнита apport-autoreport.service
-- Defined-By: systemd

-- Support: http://www.ubuntu.com/support

-- Начат процесс запуска юнита apport-autoreport.service.
апр 24 10:15:59 kamenskyhome whoopsie-upload-all[13295]: /var/crash/_usr_bin_nautilus.1000.crash already marked for upload, skipping
апр 24 10:15:59 kamenskyhome whoopsie-upload-all[13295]: /var/crash/_usr_bin_yandex-disk-indicator.1000.crash already marked for upload, skipping
апр 24 10:15:59 kamenskyhome whoopsie-upload-all[13295]: All reports processed
апр 24 10:15:59 kamenskyhome systemd[1]: Started Process error reports when automatic reporting is enabled.
-- Subject: Запуск юнита apport-autoreport.service завершен
-- Defined-By: systemd

-- Support: http://www.ubuntu.com/support

-- Процесс запуска юнита apport-autoreport.service был завершен.

-- Результат: RESULT.
апр 24 10:16:01 kamenskyhome yaIndStart[13280]: Ошибка сегментирования (core dumped)
апр 24 10:16:01 kamenskyhome runuser[13281]: pam_unix(runuser-l:session): session closed for user kamensky
апр 24 10:16:01 kamenskyhome systemd[1]: yaIndStart.service: Main process exited, code=exited, status=139/n/a
апр 24 10:16:01 kamenskyhome systemd[1]: yaIndStart.service: Failed with result 'exit-code'.
апр 24 10:16:01 kamenskyhome systemd[1]: Failed to start starts Yandex Disk Indicator when /media/kamensky/Data is mounted.
-- Subject: Ошибка юнита yaIndStart.service
-- Defined-By: systemd

-- Support: http://www.ubuntu.com/support

-- Произошел сбой юнита yaIndStart.service.

-- Результат: RESULT.
апр 24 10:16:01 kamenskyhome sudo[13277]: pam_unix(sudo:session): session closed for user root
апр 24 10:16:01 kamenskyhome systemd[1]: Starting Process error reports when automatic reporting is enabled...
-- Subject: Начинается запуск юнита apport-autoreport.service
-- Defined-By: systemd

-- Support: http://www.ubuntu.com/support

-- Начат процесс запуска юнита apport-autoreport.service.
апр 24 10:16:01 kamenskyhome whoopsie-upload-all[13299]: /var/crash/_usr_bin_nautilus.1000.crash already marked for upload, skipping
апр 24 10:16:01 kamenskyhome whoopsie-upload-all[13299]: /var/crash/_usr_bin_yandex-disk-indicator.1000.crash already marked for upload, skipping
апр 24 10:16:01 kamenskyhome whoopsie-upload-all[13299]: All reports processed
апр 24 10:16:01 kamenskyhome systemd[1]: Started Process error reports when automatic reporting is enabled.
-- Subject: Запуск юнита apport-autoreport.service завершен
-- Defined-By: systemd

-- Support: http://www.ubuntu.com/support

-- Процесс запуска юнита apport-autoreport.service был завершен.

-- Результат: RESULT.
апр 24 10:16:25 kamenskyhome sudo[13311]: kamensky : TTY=pts/0 ; PWD=/home/kamensky ; USER=root ; COMMAND=/bin/systemctl status yaIndStart.service
апр 24 10:16:25 kamenskyhome sudo[13311]: pam_unix(sudo:session): session opened for user root by (uid=0)
апр 24 10:17:01 kamenskyhome CRON[13322]: pam_unix(cron:session): session opened for user root by (uid=0)
апр 24 10:17:01 kamenskyhome CRON[13323]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
апр 24 10:17:01 kamenskyhome CRON[13322]: pam_unix(cron:session): session closed for user root
апр 24 10:17:45 kamenskyhome sudo[13311]: pam_unix(sudo:session): session closed for user root"

I know that's something I'm doing wrong-) Does anyone have an idea what exactly?

Please help-)

Comments

  • coop
    coop Posts: 915

    Please post your script. Impossible to ascertain what is wrong if we don't know what is in the script, because all the messages are telling you is that executing it failed :wink: Something which works at the command line is not guaranteed to work when it is run disconnected from a terminal; like where does the output go etc. Of what privilege level does it need etc.

  • maxim221
    maxim221 Posts: 8

    Thanx for helping me-)
    I've posted my script (actually I didn't found any instruments to highlight that ti is script),
    but it is just: "#!/bin/bash /usr/bin/yandex-disk-indicator" for nykind of reason symbol "#" is not shown properly.

  • coop
    coop Posts: 915

    Where do you expect to see the output? I think this is a graphical utility and you are trying to run it as a boot script. If you want something to run on your desktop systemd is not the way to do it, you do it with "autostart" configuration in gnome, as a startup program. Systemd and systemctl is for starting background services and daemons. So you are probably getting failure because there is no display to connect with. (SOrry I don't know yandex-disk-indicator, but it seems to be a GUI)

  • maxim221
    maxim221 Posts: 8

    I can understand what are you talking about-) But this is a program to control cloud service. And it has an auto-start option, but at the time of start the disk drive, that contain path for synchronization is not mounted yet. So I decided to write systemd unit. There is an output of this "graphical utility" if it's needed:
    "kamensky@kamenskyhome:~$ yandex-disk-indicator -l10
    2020-04-24 19:16:10,944 INFO yandex-disk-indicator v.1.11.0
    2020-04-24 19:16:10,944 DEBUG Logging level: 10
    2020-04-24 19:16:10,947 DEBUG Config value read as: autostart = False
    2020-04-24 19:16:10,947 DEBUG Config value read as: notifications = True
    2020-04-24 19:16:10,947 DEBUG Config value read as: theme = False
    2020-04-24 19:16:10,947 DEBUG Config value read as: fmextensions = True
    2020-04-24 19:16:10,947 DEBUG Config value read as: daemons = /home/kamensky/.config/yandex-disk/config.cfg
    2020-04-24 19:16:10,947 INFO Config read: /home/kamensky/.config/yd-tools/yandex-disk-indicator.conf
    2020-04-24 19:16:11,010 DEBUG Config value read as: proxy = False
    2020-04-24 19:16:11,010 DEBUG Config value read as: dir = /media/kamensky/Data/BackUpCloud
    2020-04-24 19:16:11,010 DEBUG Config value read as: auth = /home/kamensky/.config/yandex-disk/passwd
    2020-04-24 19:16:11,011 INFO Config read: /home/kamensky/.config/yandex-disk/config.cfg
    2020-04-24 19:16:11,100 INFO Daemon is already started
    2020-04-24 19:16:11,327 DEBUG Event raised by Timer
    2020-04-24 19:16:11,327 INFO Change event: stat,size,last
    2020-04-24 19:16:11,331 DEBUG Sub-menu 'Last synchronized' has 10 items
    2020-04-24 19:16:11,332 INFO Status: unknown -> idle
    "

  • luisviveropena
    luisviveropena Posts: 1,144

    Hi @maxim221 ,

    Is Yandex open source? If so, where can I find it, so I can take a look to it? I ask because it's not in the default Ubuntu repos.

    Regards,
    Luis.

  • maxim221
    maxim221 Posts: 8

    Yes. This part of product (Linux only) is opensource. Here is a gitHub link: https://github.com/slytomcat/yandex-disk-indicator

  • maxim221
    maxim221 Posts: 8

    I've changed my script to:

    "

    !/usr/bin/python

    import os
    os.system("/usr/bin/yandex-disk-indicator")
    "

    I was looking through yandex gitHub repo and managed to get that yandex indicator command waits for some kind of "python command" so I've changed bash to python in my script. "sudo systemctl start yaIndStart.service" working fine now, but I think that somthing still wrong with my systemd unit, because yandex still does not start.

    "
    kamensky@kamenskyhome:~$ sudo systemctl status yaIndStart.service
    ● yaIndStart.service - starts Yandex Disk Indicator when /media/kamensky/Data is mounted
    Loaded: loaded (/etc/systemd/system/yaIndStart.service; enabled; vendor preset: enabled)
    Active: active (exited) since Sat 2020-04-25 10:40:27 MSK; 2min 3s ago
    Process: 863 ExecStart=/usr/local/bin/yaIndStart (code=exited, status=0/SUCCESS)
    Main PID: 863 (code=exited, status=0/SUCCESS)

    апр 25 10:40:26 kamenskyhome systemd[1]: Starting starts Yandex Disk Indicator when /media/kamensky/Data is mounted...
    апр 25 10:40:26 kamenskyhome yaIndStart[863]: Unable to init server: Could not connect: Connection refused
    апр 25 10:40:26 kamenskyhome yaIndStart[863]: Unable to init server: Не удалось подключиться к: Connection refused
    апр 25 10:40:27 kamenskyhome yaIndStart[863]: Traceback (most recent call last):
    апр 25 10:40:27 kamenskyhome yaIndStart[863]: File "/usr/bin/yandex-disk-indicator", line 1308, in
    апр 25 10:40:27 kamenskyhome yaIndStart[863]: configPath = pathJoin(getenv("HOME"), '.config', appHomeName)
    апр 25 10:40:27 kamenskyhome yaIndStart[863]: File "/usr/lib/python3.6/posixpath.py", line 80, in join
    апр 25 10:40:27 kamenskyhome yaIndStart[863]: a = os.fspath(a)
    апр 25 10:40:27 kamenskyhome yaIndStart[863]: TypeError: expected str, bytes or os.PathLike object, not NoneType
    апр 25 10:40:27 kamenskyhome systemd[1]: Started starts Yandex Disk Indicator when /media/kamensky/Data is mounted.
    "

    But everything is ok if I run this script in terminal. I'm just typing : "yaIndStart" and it runs until I close term..

  • coop
    coop Posts: 915

    I'm sorry but neither of us is familiar with this program. Please pick a standard utility to start systemd with and we can help. As best I can tell you can get much if you cannot connect to a terminal. At least make your systemd job redirect output to a file that you can look at later to see what happens. Everything I read about this indicates it sets up a gui or something on your panel, which is impossible to do with systemd as a user has to be logged in for it to show and systemd runs before any user logs in.

  • maxim221
    maxim221 Posts: 8

    thank U for help-)

  • maxim221
    maxim221 Posts: 8
    edited April 2020

    But this is weird. TeamViewer, for example has it's systemd autostart service and it works fine..

  • maxim221
    maxim221 Posts: 8

    I've made it another way-)
    Modifying mount options of the disk solved my problem I've added "/dev/disk/by-id/wwn-0x50014ee2ae19e944 /mnt/wwn-0x50014ee2ae19e944 auto nosuid,nodev,nofail,x-gvfs-show 0 0" to my /etc/fstab where "wwn-0x50014ee2ae19e944" is an Id of my disk, known by "ls -la /dev/disk/by-id/" command. Thnx again.

  • luisviveropena
    luisviveropena Posts: 1,144

    It's great to know you solved it!

    Keep learning and enjoy it.

    Regards,
    Luis.

Categories

Upcoming Training