Adding service VS session LXQT AutoStart to execute my program when boots

Hi, I was trying to register my .sh shell script file;
Myprogram_Start.sh

#!/bin/sh
echo 1234  | sudo -S /home/user/MyProgram/Myprogram.sh

, to srat my shell script with sudo permission automatically with giving password using echo.

It works perfectly when I executed it by clicking mouse or using terminal.

And I wanted to execute it automatically when my device starts boot.
So I added it to systemctl service like;
//MyService.service

[Unit]

Description=Auto Start

[Service]

ExecStart=/home/user/MyProgram/Myprogram_Start.sh

[Install]

WantedBy=default.tar get

, to ~/.config/systemd/user/
directory.
And I used $ systemctl --user enable MyService.service
to add it to my user systemctl service for autostart.

But it didn’t work with this fail message by checking journalctl ;

pam_unix(sudo:session): session closed for user root
Myservice.service: Main process exited, code=exited, status=134/n/a
Myservice.service: Failed with result ‘exit-code’ .

However, when I added my Myprogram_Start.sh to lxqt session settings - auto start - LXQT session,
it auto starts properly !
But it doesn’t work well when I added it to global session like I added it to the service.

What will be the problem?
Please help me.

I have three theories:

  1. You typed something wrong (e.g. your default.tar get above)
  2. There’s maybe an error because there are cached credentials. If you sudo something, you need to enter your password. However, if you do it again right away, you don’t need to because of these cached credentials. I couldn’t reproduce this behavior, but it’s a possibility. You can use the -k switch to invalidate cached credentials.
  3. There’s something about the ultimate script (Myprogram.sh) you’re running that’s to blame.

What I can tell you is that status=134/n/a is the exit code. So you should check the exit codes for whatever command is being run. The difficulty will be figuring out which command it is if there’s a lot. To explain this consider this script:

#!/usr/bin/env bash

foo1 () { return 5; }
foo2 () { return 11; }
foo3 () { return 155; }

foo1
foo2
foo3

echo foo

So what happens here is that three exit codes are returned (5, 11, and 155, in that order), and then there’s the echo at the end. So what do you think the exit code will be? You might think 5, 11, or 155, but it’s 0, because that very last command successfully exits.

So consider the case where we change the second function to foo2 () { exit 11; }. If we were to make that change and re-run it again, what will happen? Well, there won’t be an echo. foo3 () will never run. And the ultimate exit code? 11.

Hopefully that’s enough information for you to figure this out yourself.

2 Likes

systemd doesn’t know what to do with your script.

ExecStart=bash -c '/home/user/MyProgram/Myprogram_Start.sh'

(you can also use sh -c)

2 Likes

Not only is this inconsistent with my experience, but it’s also inconsistent with the upstream documentation. Consider an example given there:

Example:

Environment=“ONE=one” ‘TWO=two two’
ExecStart=echo $ONE $TWO ${TWO}

This will execute /bin/echo with four arguments: “one”, “two”, “two”, and “two two”.

1 Like

To execute the shell script, right?
Thanks.

Thanks for your explanation.

I corrected my service to WantedBy=default.target.
Funny thing is that it goes the same.
But when I registered it to LXQT session settings - Auto Start - LXQT session,
it works !

What would be the difference?

1 Like

No clue. Without knowing what the exact content of all the scripts are, it’s pretty difficult to debug.

1 Like