Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Help with systemd service script

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Twisted Mind
Posts: 4
Joined: 2018-07-25 08:24

Help with systemd service script

#1 Post by Twisted Mind »

Hi,

I am a beginner with just basic understanding of programing. I am trying to enable a script that would copy certain files each time USB memory is mounted.
I am using OSMC version of kodi.
As i understand, there are two ways to do this, udev rules and systemd service. I have went for the systemd as it was suggested as better somewhere and it did seem simpler.

What i have done so far
1. Created trigger.sh

Code: Select all

#!/bin/sh
cp /media/Jet/{video01.mkv,video02.mkv,video03.mkv} /home/osmc/.kodi/userdata/video
Jet is the name of the usb stick. I understand it will always have to be named that way and that files will always have to be named as they are.

2. I have created unit copy.service in /etc/systemd/system

Code: Select all

[Unit]
Description=My flashdrive script trigger
Requires=media-Jet.mount
After=media-Jet.mount

[Service]
ExecStart=/home/osmc/.kodi/userdata/video/trigger.sh

[Install]
WantedBy=media-Jet.mount
3. I have executed chmod u+x for the trigger.sh

4. enabled service with sudo systemctl enable copy.service

Now, executing the trigger.sh itself works and the files are copied to the desired folder with no problems.
Mounting the usb does something, but does not copy files.
Here is the result of systemctl status copy.service

Code: Select all

osmc@osmc:~$ systemctl status copy
* copy.service - My flashdrive script trigger
   Loaded: loaded (/etc/systemd/system/copy.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2018-07-25 13:24:01 CEST; 1s ago
  Process: 841 ExecStart=/home/osmc/.kodi/userdata/video/trigger.sh (code=exited, status=1/FAILURE)
 Main PID: 841 (code=exited, status=1/FAILURE)

Jul 25 13:24:00 osmc systemd[1]: Started My flashdrive script trigger.
Jul 25 13:24:00 osmc trigger.sh[841]: cp: cannot stat '/media/Jet/{video01.mkv,video02.mkv,video03.mkv}': No such file or directory
Jul 25 13:24:01 osmc systemd[1]: copy.service: Main process exited, code=exited, status=1/FAILURE
Jul 25 13:24:01 osmc systemd[1]: copy.service: Unit entered failed state.
Jul 25 13:24:01 osmc systemd[1]: copy.service: Failed with result 'exit-code'.
During my research, i have also seen that service is running script as root and not user (osmc in this case). I have tried running trigger.sh as root (by using sudo su and then running script) and it also works with no issues.
Also worth noting is that the location of the files is not important. I have chosen this one as it is the home of playlists folder, but it can be changed to any other location.

Any help would be highly appreciated.

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Help with systemd service script

#2 Post by bw123 »

...
Now, executing the trigger.sh itself works and the files are copied to the desired folder with no problems.
...
Executing trigger.sh won't work here, I get the same error msg as the service file, because the brace expansion you are using is not portable?

Code: Select all

$ ls -l /media/foo/{foo1.ext,foo2.ext,foo3.ext}
-rwxrwxrwx 1 root root 0 Jul 25 08:29 /media/foo/foo1.ext
-rwxrwxrwx 1 root root 0 Jul 25 08:29 /media/foo/foo2.ext
-rwxrwxrwx 1 root root 0 Jul 25 08:29 /media/foo/foo3.ext
$ echo $SHELL
/bin/bash
$ sh -c "ls -l /media/foo/{foo1.ext,foo2.ext,foo3.ext}"
ls: cannot access '/media/foo/{foo1.ext,foo2.ext,foo3.ext}': No such file or directory
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jan 24  2017 /bin/sh -> dash
http://mywiki.wooledge.org/Bashism
resigned by AI ChatGPT

Twisted Mind
Posts: 4
Joined: 2018-07-25 08:24

Re: Help with systemd service script

#3 Post by Twisted Mind »

Ok, the thread can be marked as solved.
I have somehow managed to find the solution. The problem was in the cp command as it confuses the system with multiple files.
I have changed the script to

Code: Select all

#!/bin/sh
sudo cp /media/Jet/video01.mkv /home/osmc/Movies
sudo cp /media/Jet/video02.mkv /home/osmc/Movies
sudo cp /media/Jet/video03.mkv /home/osmc/Movies
This is the latest version with another directory more suitable for end user. Sudo is remaining from testing of /media folder and i dont think it is necessary for this one.

Having a command for video file that is not present on flash will result in error, but all other files will be copied. The service will not work again until rpi restart.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Help with systemd service script

#4 Post by arzgi »

Twisted Mind wrote:Ok, the thread can be marked as solved.
I have somehow managed to find the solution. The problem was in the cp command as it confuses the system with multiple files.
I have changed the script to

Code: Select all

#!/bin/sh
sudo cp /media/Jet/video01.mkv /home/osmc/Movies
sudo cp /media/Jet/video02.mkv /home/osmc/Movies
sudo cp /media/Jet/video03.mkv /home/osmc/Movies
This is the latest version with another directory more suitable for end user. Sudo is remaining from testing of /media folder and i dont think it is necessary for this one.
That could be done in one line

Code: Select all

sudo cp /media/Jet/video0?.mkv /home/osmc/Movies
? in bash is one character wildcard, * is wildcard for almost anything.

Twisted Mind
Posts: 4
Joined: 2018-07-25 08:24

Re: Help with systemd service script

#5 Post by Twisted Mind »

arzgi wrote:
Twisted Mind wrote:Ok, the thread can be marked as solved.
I have somehow managed to find the solution. The problem was in the cp command as it confuses the system with multiple files.
I have changed the script to

Code: Select all

#!/bin/sh
sudo cp /media/Jet/video01.mkv /home/osmc/Movies
sudo cp /media/Jet/video02.mkv /home/osmc/Movies
sudo cp /media/Jet/video03.mkv /home/osmc/Movies
This is the latest version with another directory more suitable for end user. Sudo is remaining from testing of /media folder and i dont think it is necessary for this one.
That could be done in one line

Code: Select all

sudo cp /media/Jet/video0?.mkv /home/osmc/Movies
? in bash is one character wildcard, * is wildcard for almost anything.
Awesome. Thanks for this. It will help since i have to include videos 01-09 and several file types like mp4, avi and mkv.
Can i use "video*" to include all numbers and types or do i have to use something like "video0?.*" ?
Oh, ad will it also work on RM command? I will have to clear the folder before copying new stuff to avoid junk in it.
Basicly, idea is

Code: Select all

#!/bin/sh
rm /home/osmc/Movies/video*
sudo cp /media/Jet/video* /home/osmc/Movies

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Help with systemd service script

#6 Post by arzgi »

Twisted Mind wrote:
Awesome. Thanks for this. It will help since i have to include videos 01-09 and several file types like mp4, avi and mkv.
Can i use "video*" to include all numbers and types or do i have to use something like "video0?.*" ?
Oh, ad will it also work on RM command? I will have to clear the folder before copying new stuff to avoid junk in it.
Basicly, idea is

Code: Select all

#!/bin/sh
rm /home/osmc/Movies/video*
sudo cp /media/Jet/video* /home/osmc/Movies
Yes, video* is enough for those, it is expanded as any file, that starts with video.

And yes to to óthrer too, rm is one those linux tools, that accept ? and *, Bash is the one wich expands those.

Twisted Mind
Posts: 4
Joined: 2018-07-25 08:24

Re: Help with systemd service script

#7 Post by Twisted Mind »

hmm, and how about using it for all the content of the folders?
Like

Code: Select all

#!/bin/sh
rm /home/osmc/Movies/*
sudo cp /media/Jet/* /home/osmc/Movies
Sorry for asing like this, but, my ideas are expanding with new knowledge :D

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Help with systemd service script

#8 Post by arzgi »

Twisted Mind wrote:hmm, and how about using it for all the content of the folders?
Like

Code: Select all

#!/bin/sh
rm /home/osmc/Movies/*
sudo cp /media/Jet/* /home/osmc/Movies
Sorry for asing like this, but, my ideas are expanding with new knowledge :D
That works too, but be careful when using

Code: Select all

rm *
that you really are in directory you mean. It really destroys everything, there is no undo option or trash bin.

Just ask, it's great you want to learn. I think there are some good bash guides on the net too. Debian pacakage abs-guide installs The Advanced Bash-Scripting Guide

Post Reply