You are not logged in.

#1 2019-12-10 14:07:40

archifer
Member
Registered: 2019-07-08
Posts: 49

[SOLVED] Autorun bash script service at boot issue

I have created a bash cleaning script which works well but I have one issue after the reboot the service won't start.

Here's the script:

#!/bin/bash
# Trash cleaning script by archifer

yes Y | sudo pacman -Sc && sudo paccache -r &&
sudo journalctl --vacuum-size=50 &&
rmlint ~/ && yes | sh ~/rmlint.sh &&
sudo rm -r ~/.cache/* &&
sudo pacman -Qtdq &&
sudo pacman -Rns $(pacman -Qtdq)

clean.service

[Unit]
Description=Clean-up old pkgs and lint

[Service]
Type=oneshot
ExecStart=/home/admin/clean.sh

[Install]
WantedBy=timers.target

clean.timer

[Unit]
Description=Clean-up old pkgs and lint

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target

Then I run

sudo systemctl enable clean.service
sudo systemctl status clean.service

And at first it worked and was active but now after the reboot I get this:

systemd[437]: clean.service: Failed to execute command
systemd[437]: clean.service: Failed at step EXEC clean.service: Failed to execute command: Permission denied
systemd[1]: clean.service: Main process exited, code clean.service: Failed at step EXEC spawning /home/admin/clean.sh: Permission denied
systemd[1]: clean.service: Failed with result 'exit-clean.service: Main process exited, code=exited, status=203/EXEC
systemd[1]: Failed to start Clean-up old pkgs and lint.

Last edited by archifer (2019-12-12 11:37:18)

Offline

#2 2019-12-10 14:21:41

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

Re: [SOLVED] Autorun bash script service at boot issue

What's the output of `stat ~/clean.sh`?

Please also edit your post to not obfuscate relevant information - your username need not be a secret, but the service file and the output clearly mismatch.

For the bash script, why are you using && at the end of every line?  If you want the whole script to stop at the first failed command, just use `set -e` at the start.  Also definitely get rid of those 'sudo's, they are pointless and just add another potential point of failure.  There's also no need to run `pacman -Qdtq` twice, just run it once in the subshell for the removal comand:

pacman -Rsn $(pacman -Qdtq)
# or if you want to script to exit without error
pacman -Rsn $(pacman -Qdtq) || true

Also, while it's not likely relevant to the current error, you could have trouble in the future if you have a seperate home partition.

Last edited by Trilby (2019-12-10 14:26:01)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2019-12-10 14:28:19

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 15,472

Re: [SOLVED] Autorun bash script service at boot issue

I'm almost certain services in system mode are supposed to run as root , not as user.
Your script combines acts as root with acts as user.

I suggest you create 2 scripts : one for root and another for user.
https://wiki.archlinux.org/index.php/Systemd/User may be useful for you.

Apart from that , there's one command in your script I would never automate.

pacman -Rns $(pacman -Qtdq)

I run the pacman -Qtd manually often and have found it regularly shows things I do not want to remove because I still need them (I change the install reason to explicit then to make sure they'll no longer be listed by -Qdt)

If you do want to keep this automated, atleast remove the s  from pacman -Rns .

Last edited by Lone_Wolf (2019-12-10 14:30:35)


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

#4 2019-12-10 14:55:24

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

Trilby wrote:

What's the output of `stat ~/clean.sh`?

Please also edit your post to not obfuscate relevant information - your username need not be a secret, but the service file and the output clearly mismatch.

For the bash script, why are you using && at the end of every line?  If you want the whole script to stop at the first failed command, just use `set -e` at the start.  Also definitely get rid of those 'sudo's, they are pointless and just add another potential point of failure.  There's also no need to run `pacman -Qdtq` twice, just run it once in the subshell for the removal comand:

pacman -Rsn $(pacman -Qdtq)
# or if you want to script to exit without error
pacman -Rsn $(pacman -Qdtq) || true

Also, while it's not likely relevant to the current error, you could have trouble in the future if you have a seperate home partition.

Yes I have seperate home

here's the output for the stat

stat ~/clean.sh
  File: /home/admin/clean.sh
  Size: 248       	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 3407964     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  admin)   Gid: ( 1000/  admin)

Offline

#5 2019-12-10 14:57:21

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

Lone_Wolf wrote:

I'm almost certain services in system mode are supposed to run as root , not as user.
Your script combines acts as root with acts as user.

I suggest you create 2 scripts : one for root and another for user.
https://wiki.archlinux.org/index.php/Systemd/User may be useful for you.

Apart from that , there's one command in your script I would never automate.

pacman -Rns $(pacman -Qtdq)

I run the pacman -Qtd manually often and have found it regularly shows things I do not want to remove because I still need them (I change the install reason to explicit then to make sure they'll no longer be listed by -Qdt)

If you do want to keep this automated, atleast remove the s  from pacman -Rns .

Thanks, I will check it.

Offline

#6 2019-12-10 15:09:38

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

Trilby wrote:

What's the output of `stat ~/clean.sh`?

Please also edit your post to not obfuscate relevant information - your username need not be a secret, but the service file and the output clearly mismatch.

For the bash script, why are you using && at the end of every line?  If you want the whole script to stop at the first failed command, just use `set -e` at the start.  Also definitely get rid of those 'sudo's, they are pointless and just add another potential point of failure.  There's also no need to run `pacman -Qdtq` twice, just run it once in the subshell for the removal comand:

pacman -Rsn $(pacman -Qdtq)
# or if you want to script to exit without error
pacman -Rsn $(pacman -Qdtq) || true

Also, while it's not likely relevant to the current error, you could have trouble in the future if you have a seperate home partition.

I have changed the permission of clean.sh to 755 now the process gets enabled but failed to start

systemctl status clean.service
● clean.service - Clean-up old pkgs and lint
     Loaded: loaded (/etc/systemd/system/clean.service; enabled; vendor preset:disabled)
     Active: failed (Result: exit-code) since Tue 2019-12-10 17:05:33 EET; 15s ago
    Process: 15034 ExecStart=/home/admin/clean.sh (code=exited, status=127)
   Main PID: 15034 (code=exited, status=127)

Offline

#7 2019-12-10 15:25:13

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

Re: [SOLVED] Autorun bash script service at boot issue

Well yes, that's because your script as written makes almost no sense at all as noted by the previous posts in this thread.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2019-12-10 15:35:44

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

Trilby wrote:

Well yes, that's because your script as written makes almost no sense at all as noted by the previous posts in this thread.

The idea was to launch these commands weekly

pacman -Sc
paccache -r
journalctl --vacuum-size=50
rmlint /home/admin/
sh /home/admin/rmlint.sh
pacman -R $(pacman -Qtdq)

Because I do run those commands every week or twice a week to keep the system clean and I just wanted to make them automated.

Offline

#9 2019-12-10 16:18:49

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,171

Re: [SOLVED] Autorun bash script service at boot issue

What does the actual script look like atm, what is "/home/admin/rmlint.sh" (stat and content)?
"status=127" means that some command was not found.

Offline

#10 2019-12-10 17:05:48

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

seth wrote:

What does the actual script look like atm, what is "/home/admin/rmlint.sh" (stat and content)?
"status=127" means that some command was not found.

rmlint is a script for cleaning broken links and leftovers lint I just installed it with `pacman -S rmlint` it works good.

Offline

#11 2019-12-10 17:07:42

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,171

Re: [SOLVED] Autorun bash script service at boot issue

I know what rmlint is, but you seem to be calling some script in your $HOME, also please answer the other question.

Offline

#12 2019-12-10 17:40:31

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

seth wrote:

I know what rmlint is, but you seem to be calling some script in your $HOME, also please answer the other question.

`cleaner.sh` as in my post that is a script with the set of commands that I created personally when it is run it creates `rmlint.sh` during the clean process as per `rmlint ~/` command and then executes it. Both .sh are executable and they work if I run `sh ~/clean.sh` manually in terminal all commands are executed there are no errors and if I create a service with this script (clean.sh) I can enable that service once created `sudo nano /etc/systemd/system/clean.service` but I cannot start it after that with sudo systemctl start clean.service as it gives error all the time.

Last edited by archifer (2019-12-10 17:41:00)

Offline

#13 2019-12-10 17:45:00

loqs
Member
Registered: 2014-03-06
Posts: 19,097

Re: [SOLVED] Autorun bash script service at boot issue

What do you think ~ expands to when the script is executed by the service?

Offline

#14 2019-12-10 17:51:07

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

loqs wrote:

What do you think ~ expands to when the script is executed by the service?

I have replaced it to `rmlint /home/admin/`(instead) but it still won't do. It is weird because first time I created it, it was active process but after reboot I cannot start it anymore.

Offline

#15 2019-12-10 17:55:19

loqs
Member
Registered: 2014-03-06
Posts: 19,097

Re: [SOLVED] Autorun bash script service at boot issue

What is the contents of the script now and what is the service's status?
Have you also removed all the superfluous sudo usage?

Last edited by loqs (2019-12-10 17:55:40)

Offline

#16 2019-12-10 18:07:02

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

loqs wrote:

What is the contents of the script now and what is the service's status?
Have you also removed all the superfluous sudo usage?

Changed it like this

#!/bin/bash
# Trash cleaning script by archifer

yes Y | pacman -Sc 
paccache -r
journalctl --vacuum-size=50
rmlint /home/admin/ && yes | sh /home/admin/rmlint.sh
rm -r /home/admin/.cache/*
pacman -Qtdq && pacman -R $(pacman -Qtdq)

and if I run `systemctl start clean.service`

clean.service - Clean-up old pkgs and lint
     Loaded: loaded (/etc/systemd/system/clean.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Tue 2019-12-10 20:01:57 EET; 1min 36s ago
TriggeredBy: ● clean.timer
    Process: 19720 ExecStart=/home/admin/clean.sh (code=exited, status=1/FAILURE)
   Main PID: 19720 (code=exited, status=1/FAILURE)

Last edited by archifer (2019-12-10 18:08:37)

Offline

#17 2019-12-10 18:39:17

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

Ok it works now, thanks all for helping out, I used @Trilby suggestion to remove sudos and add `pacman -R $(pacman -Qtdq) || true` other issue was that `rmlint` didn't update .sh in home directory but created separate file in root directory so I had to change the path.

#!/bin/bash
# Trash cleaning script by archifer

yes Y | pacman -Sc 
paccache -r
journalctl --vacuum-size=50
rmlint /home/admin/ && yes | sh /rmlint.sh
rm -r /home/admin/.cache/*
pacman -R $(pacman -Qtdq) || true

Offline

#18 2019-12-10 19:29:15

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

Re: [SOLVED] Autorun bash script service at boot issue

I'm glad the script is working now, but ...

archifer wrote:

The idea was to launch these commands weekly

Then why have you set the service to run at boot rather than once a week?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#19 2019-12-10 21:18:27

archifer
Member
Registered: 2019-07-08
Posts: 49

Re: [SOLVED] Autorun bash script service at boot issue

Trilby wrote:

I'm glad the script is working now, but ...

archifer wrote:

The idea was to launch these commands weekly

Then why have you set the service to run at boot rather than once a week?

That was probably my mistake I understood the procedure somewhat different but now it's all cleared out.

Offline

#20 2019-12-10 23:01:14

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,648

Re: [SOLVED] Autorun bash script service at boot issue

Please remember to mark your thread [SOLVED] (edit the title of your first post).

Offline

Board footer

Powered by FluxBB