You are not logged in.

#1 2020-01-16 03:43:10

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

[SOLVED] .timer and .service

I have two questions regarding systemd/timers that I have not been able to resolve using the wiki:

I would like my service to run a python script, using a virtual environment, every ten minutes.  My timer is saved under /home/Eric/path/to/file/exchange.timer and reads:

[Unit]
Description=Checks the exchange rate of Bitcoin/USD every 10 minutes and makes buy/sell decisions

[Timer]
OnCalendar=*-*-* *:0,10,20,30,40,50

[Install]
WantedBy=timers.target

The service I wrote is under /home/Eric/path/to/file/exchange.service (the timer and the service are in the same folder) and reads:

[Unit]
Description= Check exchange rate for Bitcoin/USD every 10 minutes and make buy/sell decisions

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=source /home/eric/A/Automations/BitcoinExchange/environment/bin/activate
ExecStart=python /home/The-Index/A/Automations/BitcoinExchange/automate.py
ExecStart=deactivate

When I run

 sudo systemctl start /home/Eric/path/to/file/exchange.service 

or

 sudo systemctl start /home/Eric/path/to/file/exchange.timer 

I receive the error:

Failed to start home-eric-path-to-file-exchange.timer.mount: Unit home-eric-path-to-file-exchange.timer.mount not found

Failed to start home-eric-path-to-file-exchange.service.mount: Unit home-eric-path-to-file-exchange.service.mount not found

I'm not sure what it is asking me to mount.  Also, I'm not sure if my .service file is even valid.  Will it execute each ExecStart line?  Those commands together do what I want when I run them, but I'm not certain the syntax is right for a service file.

I appreciate any help!

Best,
Eric

Last edited by eric.meehan (2020-01-16 17:08:42)

Offline

#2 2020-01-16 03:45:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] .timer and .service

You enable/start the timer, not the service. And --user services are located in ~/.config/systemd/user
https://wiki.archlinux.org/index.php/Systemd/User


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2020-01-16 03:51:53

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

jasonwryan wrote:

You enable/start the timer, not the service. And --user services are located in ~/.config/systemd/user
https://wiki.archlinux.org/index.php/Systemd/User

Thank you, you mean that I should move the service into that folder?  Should the timer be there as well?

Edit: There was not a directory ~/.config/systemd/user.  I created the path, then moved the files there and tried to start the timer again.  Same problem.

Last edited by eric.meehan (2020-01-16 03:57:41)

Offline

#4 2020-01-16 04:06:59

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] .timer and .service

Please paste exact comands and full output: https://bbs.archlinux.org/viewtopic.php?id=57855


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2020-01-16 04:24:21

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

After running

sudo systemctl enable --user exchange.timer

and then

systemctl status exchange.timer

I found that the timer is in fact loaded

Loaded: loaded (/home/eric/A/Automations/BitcoinExchange/exchange.timer; enabled; vendor preset: disabled)
     Active: inactive (dead)
    Trigger: n/a
   Triggers: * exchange.service

When I ran

sudo systemctl start exchange.timer

I received the error

Job failed. See "journalctl -xe" for details.

Checking journalctl, I found the error

Jan 15 22:59:44 Hangar-Bay systemd[17845]: Configuration file /home/eric/.config/systemd/user/exchange.timer is marked executable. Please remove executable permission bits. Proceeding anyway.
Jan 15 22:59:44 Hangar-Bay systemd[17845]: Configuration file /home/eric/.config/systemd/user/exchange.service is marked executable. Please remove executable permission bits. Proceeding anyway.
Jan 15 22:59:44 Hangar-Bay systemd[17845]: /home/eric/.config/systemd/user/exchange.service:7: Executable "source" not found in path "/usr/local/bin:/usr/bin"
Jan 15 22:59:44 Hangar-Bay systemd[17845]: exchange.service: Unit configuration has fatal error, unit will not be started.
Jan 15 22:59:44 Hangar-Bay systemd[17845]: exchange.timer: Refusing to start, unit exchange.service to trigger not loaded.

The part saying "source" not found is confusing me, as the full command

source /home/Eric/path/to/python/virtualenvironment/bin/activate

works fine, activating my virtual environment where the needed packages are installed. 

I am also not sure what the executable permission bits are referring to.

Last edited by eric.meehan (2020-01-16 04:29:58)

Offline

#6 2020-01-16 04:43:00

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] .timer and .service

Did you read any of the wiki page I linked to?

systemctl --user enable foo.timer

Don't randomly insert sudo into commands in the deluded hope that it will somehow make things work. Privileges should only ever be elevated when you know what you are doing.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2020-01-16 04:44:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] .timer and .service

There are several things wrong here.  Now you have the files in the right place, but stop using sudo for user services.  Your OnCalendar will likely work, but if you really want it to run every 10 minutes, why not just use OnUnitActiveSec=600?

Now for the critical error(s): 'source' is a shell command, not a binary.  There is no executable on the system named 'source'.  So that will not work.

Even if there was such a binary, it would not have the result you'd want: I gather you want the content of the environment imported to the same environment that runs the python script.  As written this will not happen.  Check the systemd wiki or man pages on setting environment variables for services.

eric.meehan wrote:

I am also not sure what the executable permission bits are referring to.

I don't mean to offend, but if this is not clear, you really should not be using a distro like arch linux.  File permissions are pretty basic *nix concepts.  If you intend to continue with arch, you'll need to devote some time to reading up on the basics.  I'd suggest the following as a starting point:
https://ryanstutorials.net/linuxtutorial/

Last edited by Trilby (2020-01-16 04:47:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2020-01-16 04:56:23

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [SOLVED] .timer and .service

eric.meehan wrote:

The part saying "source" not found is confusing me, as the full command

source /home/Eric/path/to/python/virtualenvironment/bin/activate

works fine, activating my virtual environment where the needed packages are installed. 

I am also not sure what the executable permission bits are referring to.

systemd units don't run their ExecStart inside of a bash shell, they use the exec(3)  family of functions. Also, the "source" builtin is bash-specific, so in cases where /bin/sh is used, you should use ". /home/Eric/path/to/python/virtualenvironment/bin/activate" This won't work either:

import subprocess

subprocess.run(['.', '/home/Eric/path/to/python/virtualenvironment/bin/activate'], shell=False)

But this will:

import subprocess

subprocess.run('. /home/Eric/path/to/python/virtualenvironment/bin/activate', shell=True)

See the COMMAND LINES section in the systemd.service(5) man page.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#9 2020-01-16 05:05:01

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

Trilby wrote:

There are several things wrong here.  Now you have the files in the right place, but stop using sudo for user services.  Your OnCalendar will likely work, but if you really want it to run every 10 minutes, why not just use OnUnitActiveSec=600?

Now for the critical error(s): 'source' is a shell command, not a binary.  There is no executable on the system named 'source'.  So that will not work.

Even if there was such a binary, it would not have the result you'd want: I gather you want the content of the environment imported to the same environment that runs the python script.  As written this will not happen.  Check the systemd wiki or man pages on setting environment variables for services.

eric.meehan wrote:

I am also not sure what the executable permission bits are referring to.

I don't mean to offend, but if this is not clear, you really should not be using a distro like arch linux.  File permissions are pretty basic *nix concepts.  If you intend to continue with arch, you'll need to devote some time to reading up on the basics.  I'd suggest the following as a starting point:
https://ryanstutorials.net/linuxtutorial/

That is a more efficient way to get the 10 minute timer, thank you.

You are correct on my intents for the environment.  Rather than installing python libraries system wide, I am using a virtual environment that isolates those installations from the rest of my system.  Entering the environment using the "source" command above allows me to access those libraries once I run a python script.  Looking at the link you shared, it seems I need something like this:

[Service]
Environment=SYSTEMD_LOG_LEVEL=debug

For my purposes, would I write it like this?

Environment=/home/Eric/path/to/virtualenvironment/bin/activate

Keeping in mind that "activate" is a script, not a directory.

Also, I appreciate your point, and will take a look at the tutorial you shared; however, I was simply not being clear with my confusion.  I understand file permissions, I just didn't know what line in my .timer and .service file that particular line was referring to.  Where is it marked executable?

Thanks again for your help!

Offline

#10 2020-01-16 05:10:15

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

eschwartz wrote:
eric.meehan wrote:

The part saying "source" not found is confusing me, as the full command

source /home/Eric/path/to/python/virtualenvironment/bin/activate

works fine, activating my virtual environment where the needed packages are installed. 

I am also not sure what the executable permission bits are referring to.

systemd units don't run their ExecStart inside of a bash shell, they use the exec(3)  family of functions. Also, the "source" builtin is bash-specific, so in cases where /bin/sh is used, you should use ". /home/Eric/path/to/python/virtualenvironment/bin/activate" This won't work either:

import subprocess

subprocess.run(['.', '/home/Eric/path/to/python/virtualenvironment/bin/activate'], shell=False)

But this will:

import subprocess

subprocess.run('. /home/Eric/path/to/python/virtualenvironment/bin/activate', shell=True)

See the COMMAND LINES section in the systemd.service(5) man page.

Thank you for the input.  Is the code you shared supposed to go in my .service file?  I was unable to find a systemd.service(5) page nor a command lines section within systemd on the wiki.  Do you have a link to what you are referring to?

Offline

#11 2020-01-16 05:11:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] .timer and .service

eric.meehan wrote:

I understand file permissions... Where is it marked executable?

Asking this question negates the statement.  There is no line in a file that marks it as executable - that is not how permissions work.

As for the virtualenv - I don't have much experience with them.  If the "activate" file is a script, then it would not work quite like the environment variable examples in the documentation.  You'd either 1) need to run the activate script and python script in the same shell invocation (to preserve the environment) or 2) dissect the activate script to get the actual environment variables that need to be set and put those in the service file.  I'd prefer the latter, but - again - I don't use virtual envs, so the former would look something like this:

ExecStart=/bin/sh -c "/path/to/activate; python /path/to/script.py"

Though I think the core functionality of virtual environment activation is just prepending their own bin path to the PATH environment variable.  This could very easily be done in a service file:

Environment="PATH=/path/to/venv/bin:/usr/bin"
ExecStart=python /path/to/script.py

Last edited by Trilby (2020-01-16 05:16:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#12 2020-01-16 05:11:30

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

jasonwryan wrote:

Did you read any of the wiki page I linked to?

systemctl --user enable foo.timer

Don't randomly insert sudo into commands in the deluded hope that it will somehow make things work. Privileges should only ever be elevated when you know what you are doing.

Seeing as I was trying to run a shell command in the .service file, I don't think this was the problem

Offline

#13 2020-01-16 05:15:14

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

Trilby wrote:
eric.meehan wrote:

I understand file permissions... Where is it marked executable?

Asking this question negates the statement.  There is no line in a file that marks it as executable - that is not how permissions work.

Haha fair enough.  I prefer to learn in this sort of environment though, so I appreciate your patience! 

Trilby wrote:

As for the virtualenv - I don't have much experience with them.  If the "activate" file is a script, then it would not work quite like the environment variable examples in the documentation.  You'd either 1) need to run the activate script and python script in the same shell invocation (to preserve the environment) or 2) dissect the activate script to get the actual environment variables that need to be set and put those in the service file.  I'd prefer the latter, but - again - I don't use virtual envs, so the former would look something like this:

ExecStart=/bin/sh -c "/path/to/activate; python /path/to/script.py"

I will give that a try and report back on the results.  Many thanks for the assistance!

Offline

#14 2020-01-16 05:16:51

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] .timer and .service

eric.meehan wrote:
jasonwryan wrote:

Did you read any of the wiki page I linked to?

systemctl --user enable foo.timer

Don't randomly insert sudo into commands in the deluded hope that it will somehow make things work. Privileges should only ever be elevated when you know what you are doing.

Seeing as I was trying to run a shell command in the .service file, I don't think this was the problem

Not reading the page was/is definitely a big part of the problem. Randomly using sudo is a symptom.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#15 2020-01-16 05:17:31

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [SOLVED] .timer and .service

eric.meehan wrote:

You are correct on my intents for the environment.  Rather than installing python libraries system wide, I am using a virtual environment that isolates those installations from the rest of my system.  Entering the environment using the "source" command above allows me to access those libraries once I run a python script.  Looking at the link you shared, it seems I need something like this:

[Service]
Environment=SYSTEMD_LOG_LEVEL=debug

For my purposes, would I write it like this?

Environment=/home/Eric/path/to/virtualenvironment/bin/activate

Keeping in mind that "activate" is a script, not a directory.

More importantly, "activate" is not the literal value of an environment variable; how exactly do you propose parsing the filepath as a key=value pair for the Environment setting?

Did you read the systemd.exec(3) manpage to understand the meaning of the Environment setting, and also the EnvironmentFile setting, and why neither of them are at all what you want to do?

The thing is that a virtualenv "activate" script is really boring and just sets some fancy shell prompt that you don't need, plus adds /home/Eric/path/to/virtualenvironment/bin/ to your $PATH. You don't need that script at all, just use /home/Eric/path/to/virtualenvironment/bin/python directly.

Also, I appreciate your point, and will take a look at the tutorial you shared; however, I was simply not being clear with my confusion.  I understand file permissions, I just didn't know what line in my .timer and .service file that particular line was referring to.  Where is it marked executable?

Thanks again for your help!

Uhhhhhhhhh.

Configuration file /home/eric/.config/systemd/user/exchange.service is marked executable. Please remove executable permission bits.

The file .service is marked executable if you use the "ls -l" command to view the file. Do you suppose that file permissions are defined by a line of text within the file?

Your question is confusing and bizarre.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#16 2020-01-16 12:29:44

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

eschwartz wrote:
eric.meehan wrote:

You are correct on my intents for the environment.  Rather than installing python libraries system wide, I am using a virtual environment that isolates those installations from the rest of my system.  Entering the environment using the "source" command above allows me to access those libraries once I run a python script.  Looking at the link you shared, it seems I need something like this:

[Service]
Environment=SYSTEMD_LOG_LEVEL=debug

For my purposes, would I write it like this?

Environment=/home/Eric/path/to/virtualenvironment/bin/activate

Keeping in mind that "activate" is a script, not a directory.

More importantly, "activate" is not the literal value of an environment variable; how exactly do you propose parsing the filepath as a key=value pair for the Environment setting?

Did you read the systemd.exec(3) manpage to understand the meaning of the Environment setting, and also the EnvironmentFile setting, and why neither of them are at all what you want to do?

The thing is that a virtualenv "activate" script is really boring and just sets some fancy shell prompt that you don't need, plus adds /home/Eric/path/to/virtualenvironment/bin/ to your $PATH. You don't need that script at all, just use /home/Eric/path/to/virtualenvironment/bin/python directly.

Also, I appreciate your point, and will take a look at the tutorial you shared; however, I was simply not being clear with my confusion.  I understand file permissions, I just didn't know what line in my .timer and .service file that particular line was referring to.  Where is it marked executable?

Thanks again for your help!

Uhhhhhhhhh.

Configuration file /home/eric/.config/systemd/user/exchange.service is marked executable. Please remove executable permission bits.

The file .service is marked executable if you use the "ls -l" command to view the file. Do you suppose that file permissions are defined by a line of text within the file?

Your question is confusing and bizarre.

I wouldn't need to run the activate script if the package required to run the python script was installed system wide.  I am using a library, alpha_vantage, for python that I have installed in a virtual environment.  Without activating it, my script will not run.  You're right, I wouldn't need it if I just installed it system wide, but that is not really the best practice for python. 

I have read the systemd page on the wiki, but searching for systemd.exec(3) or, as another user suggested, systemd.exec(5) does not return any pages.  Could you share a link to the page you are referring to?

Frankly, I know I don't know what I'm doing here.  Its fine.  I'm not running this on computers with files from the Pentagon. I am free to ruin the system and reinstall the os.  There is nothing really important on it.  It seems only natural to me that I would ask questions that don't make sense, its a great way to learn what you don't know. It is especially helpful when people tell me why my question does not make sense.  This is more directed to Anarchist than you, as your comment was helpful, but simply saying I don't know what I'm doing is, as Anarchist's link itself states, "probably the most useless thing you could post".

Offline

#17 2020-01-16 12:39:20

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,428

Re: [SOLVED] .timer and .service

Eschwartz did specify man page, so read the man page...

man 3 systemd.exec

Last edited by V1del (2020-01-16 15:04:53)

Offline

#18 2020-01-16 13:34:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] .timer and .service

eric.meehan wrote:

Frankly, I know I don't know what I'm doing here.  Its fine ... its a great way to learn what you don't know. It is especially helpful when people tell me why my question does not make sense.

You're missing the point.  It's not fine.  You are in the wrong place.  I tried to suggest this gently before.

I started writing up an analogy here when I remembered there is already a perfectly fitting and well written one by Xyne here.

You're holding up traffic in the crawling lane expecting everyone there to cater to your needs.  You may have felt that Jason ("The Anarchist") was abrupt, but he is not here to serve just you, his contributions are for the maintenance and betterment of the whole community.  On occasion, the best response is to tell someone that they are trying to swim in the wrong part of the pool.

Your assertitions that "it's fine" because you don't have anything critical on your system is like the novice swimmer ignoring complains about their presence by saying "I don't care if I'm putting myself at risk, if I get injured, that's on me."  But it isn't.  You are getting in the way of many other crawlers.  Failing to recognize your impact on others is a very shortsighted and selfish perspective.

These forums are here to help archers.  But we are not here to cater to every whim regardless of whether it is appropriately targetted in this community or not.

Really, for your own benefit get expereince with another distro before you use Arch.  And if not for your benefit, for ours.  Do not waste the goodwill and effort of this community when it could be much more useful to others.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#19 2020-01-16 14:17:32

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [SOLVED] .timer and .service

To run a python program in a virtual env from a script or service you don't use activate at all. Use `path/to/env/bin/python path/to/script`.

Offline

#20 2020-01-16 14:45:06

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [SOLVED] .timer and .service

eric.meehan wrote:

I wouldn't need to run the activate script if the package required to run the python script was installed system wide.  I am using a library, alpha_vantage, for python that I have installed in a virtual environment.  Without activating it, my script will not run.  You're right, I wouldn't need it if I just installed it system wide, but that is not really the best practice for python.

I never said anything about installing it system wide, so I'm not sure what your response is supposed to mean.

Honestly, it looks like you didn't read my post at all, then tried dodging the question by making some strawman argument up to respond to. I hope that is not true, because if it were true, then we'd have nothing else to discuss.

Please reread my post and respond to the actual things which I said, if you want to discuss its content with me.

I have read the systemd page on the wiki, but searching for systemd.exec(3) or, as another user suggested, systemd.exec(5) does not return any pages.  Could you share a link to the page you are referring to?

https://lmgtfy.com/?q=systemd.exec(3)

I'll admit to having typoed it, it is actually systemd.exec(5) - fortunately either one finds you the manpage in question via google. Did you try that?

You did not try finding it with the "man" program, or you would have found it when trying the section "5" version. Are you familiar with the use of Unix manpages?

Frankly, I know I don't know what I'm doing here.  Its fine.  I'm not running this on computers with files from the Pentagon. I am free to ruin the system and reinstall the os.  There is nothing really important on it.  It seems only natural to me that I would ask questions that don't make sense, its a great way to learn what you don't know.

No, it is not fine. https://wiki.archlinux.org/index.php/Fr … I_use_Arch?

FAQ wrote:

If you are a beginner and want to use Arch, you must be willing to invest time into learning a new system, and accept that Arch is designed as a 'do-it-yourself' distribution; it is the user who assembles the system.

Before asking for help, do your own independent research by Googling, searching the forum and the superb documentation provided by the Arch Wiki.

It's fine to not know how things work. It's not fine to expect the Arch community to walk you through the simplest of things, such as how to read documentation. There are other excellent distributions and communities for that -- this is not one of them, this community specializes in enabling users to be their own troubleshooter, not in producing "Linux 101" walkthroughs.

All you will accomplish is frustrating other users, who are not impressed with how little you care about the community and attempt to trivialize it via bad jokes about the Pentagon.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#21 2020-01-16 17:06:00

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

I've found the solution to my problem.  Thanks again to all who tried to help.  I'm going to be as explicit as possible with my problems and the solutions for anyone who views this in the future.

What I trying to do:
I wanted a .timer file to execute a .service file of my own design every 10 minutes.  On activation, the service file would enter a virtual environment for python, run the script I wrote, and then deactivate the virtual environment.

What I was doing wrong:
First and foremost, as pointed out by jasonwryan, I had my .timer and .service files in the wrong place.  They were in:

/home/Eric/path/to/python/script/exchange.timer
/home/Eric/path/to/python/script/exchange.service

When they should have been in

/home/Eric/.config/systemd/user/exchange.timer
/home/Eric/.config/systemd/user/exchange.service

Moreover, my method of activating them was incorrect as well.  I was using

sudo systemctl enable exchange.service

which Jason corrected to:

systemctl --user enable exchange.timer

No sudo, specifying --user, and activating .timer instead of .service.  Thank you Jason for the input, I apologize for becoming short with you.  You were right, I was haphazardly entering commands without knowing what they would do, and your advice was helpful to getting me on the right track.  I appreciate your input.

Next was the .service file, in which I was trying to run three separate commands to enter the virtualenv, run the script, and then deactivate the virtualenv.

ExecStart=source /path/to/environment/bin/activate
ExecStart=python /path/to/script.py
ExecStart=deactivate

This was totally wrong, as pointed out by Trilby, who suggested I concatenate my commands to a single line and activate the environment using

ExecStart=/bin/sh -c "/path/to/activate; python /path/to/script.py"

This, however, was only part of the solution.  Eschwartz had the simplest solution - there was no need to activate the virtualenv at all.  I could simply specify running the script with the version of python in my environment folder:

ExecStart=/home/Eric/path/to/environment/python /home/Eric/path/to/script.py

The timer and service are both working now.  Thank you to everyone who helped me, I appreciate your time.

I still have a bit of a problem though.  The python file is under the path /home/Eric/path/to/script.py, and should create text files with output in the same directory.  However, the automation writes those text files to /home/Eric instead.  In python, a script such as

file = open('data.txt', 'w')

should write to the same directory that the script is written in, since no absolute path is specified.  Should I post this in a new thread, or is appropriate to continue here?

Last edited by eric.meehan (2020-01-16 17:06:16)

Offline

#22 2020-01-16 17:15:15

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [SOLVED] .timer and .service

eric.meehan wrote:

I've found the solution to my problem.  Thanks again to all who tried to help.  I'm going to be as explicit as possible with my problems and the solutions for anyone who views this in the future.

Thanks.

I still have a bit of a problem though.  The python file is under the path /home/Eric/path/to/script.py, and should create text files with output in the same directory.  However, the automation writes those text files to /home/Eric instead.  In python, a script such as

file = open('data.txt', 'w')

should write to the same directory that the script is written in, since no absolute path is specified.  Should I post this in a new thread, or is appropriate to continue here?

You could also tell your ExecStar=sh -c "..." command to first "cd /home/Eric/path/to/".

Alternatively, your python script could be made resilient to being run in systemd services and other cases where the working directory is not set, by telling the script the exact location of the file, you can do that in python by using os.path.join(os.path.dirname(__file__), 'data.txt') as the filename.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#23 2020-01-16 17:32:40

walteweiss
Member
Registered: 2019-09-07
Posts: 32

Re: [SOLVED] .timer and .service

Hello everyone, I have a similar, yet different question on here. It corresponds to the title and Newbie Corner, and maybe will help the OP to understand this part as well.

Alothough, my questions are slightly different, yet related to ‘.timer and .service’ topic name.

I have a script I want to run daily to create a folder, the bash command is

mkdir $(date +"%m-%d-%a")

which creates a dir named 01-16-Thu. I don’t understand how to make it, after reading systemd/Timers page. Should I create it myself, or does systemd handles it by itself? I think it’s not the dumb question, because before I tried to edit a systemd unit, not noticing it should be edit with

systemctl edit name.service

I have no idea whether I was correct on my manual change, as it was mentioned in another wiki entry, seems like it was. But I removed my manual changes and did them as it’s written on the wiki. I see no command for that. My command is simple, but as I understand Transient.timer units part, it’s for one-time events, and that can be run without a service file. But even if my command quite simple, I do need to have a file (two of them: .timer and .service), am I correct here?

I don’t understand whether I should run my timer as my user or as root? I need to create that directory in my home folder, but it’s going to be on a headless server, and I don’t understand how users are handled there, when I have no SSH-connection. Say, will my --user unit start when I do login, or it will also start after reboot, by itself? I have read systemd/User page and it’s still not clear to me.


Should I mark those two — .service and .timer — files as executables or not?

chmod u+x path/to/file

The problem with all that wiki wisdom as it’s not that clear before you actually try, and for some things it’s just quicker to confirm someone who already tried that, rather than experimenting on your own.

I know how to use crontab, and it’s easy. As it’s recommended to stick to the systemd.timers, I would like to learn how to use it, as it looks simpler from the first glance. But the manual is complicated.


Russia is committing genocide on Ukraine right now (2022—2023), please help Ukraine as much as you can. That’s the turning point of the democracy vs tyranny war, and if Ukraine loses, everyone in the democracy world will. You can donate to Ukraine here: savelife.in.ua/en/donate or help with spreading the information about the ongoing events.

Offline

#24 2020-01-16 18:05:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] .timer and .service

eric.meehan, thank you for your last post.  It seems you are absorbing what's being thrown at you, and you shared your progress which can be a benefit to the community.  These are signs that you can do well in this community.

However, I stand by my other assessments that you are over your head using arch.  You will need to invest time and energy building up a knowledge of linux basics.  If you work through the tutorial I linked to earlier, it should help bring you up to speed.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#25 2020-01-16 20:01:57

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: [SOLVED] .timer and .service

Trilby wrote:

eric.meehan, thank you for your last post.  It seems you are absorbing what's being thrown at you, and you shared your progress which can be a benefit to the community.  These are signs that you can do well in this community.

However, I stand by my other assessments that you are over your head using arch.  You will need to invest time and energy building up a knowledge of linux basics.  If you work through the tutorial I linked to earlier, it should help bring you up to speed.

I appreciate your help and will definitely work through the tutorial you shared.  As I said, this is just my preferred method of learning.  I like to jump into the deep end and figure out how to swim.  While this may not be a problem, perhaps the forums are not the place to exercise that method.  Hopefully my future posts won't be so messy.

Best,
Eric

Offline

Board footer

Powered by FluxBB