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

 

 

 

[Bash] Run script on Resume or on Login - breaking my head

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Wm. A. Weasel
Posts: 31
Joined: 2014-11-22 02:59
Location: Wisconsin USA
Has thanked: 7 times

[Bash] Run script on Resume or on Login - breaking my head

#1 Post by Wm. A. Weasel »

I’m a little embarrassed to ask this, because there are at least dozens of answers online for this question, but I can’t find one that works.

I’m running Debian Bullseye. I have a super-simple script that connects the computer to a bluetooth receiver and sets that receiver to be the default pulseaudio “sink”. I want the script to run when the computer resumes from suspend, or when I log in - I don’t really care which.

Here’s the script:

Code: Select all

#!/bin/bash 
touch /home/me/BTC1
# The above line is to create a “footprint” to show whether the script has executed.
bluetoothctl < /usr/local/bin/BTconnect.txt
sleep 8
# The sleep command is because sometimes the next line, 
#    the pacmd, would report that the sink doesn’t exist.
#    I’m not sure the delay cures it, but that's a different issue.
pacmd set-default-sink bluez_sink.NN_NN_NN_NN_NN_NN.a2dp_sink 
# The NN's aren't the real address of course. 
exit 0
It works, at least most of the time, when I run it from a terminal. (See comment in code for the exceptions.) Numerous posts instruct me to put the script in various directories, and it will be executed either on resume or on login. I’ve tried placing it in:
/lib/systemd/system-sleep/
/usr/lib/pm-utils/sleep.d/
/etc/profile.d/


I tried appending the guts of the script to an existing file, /home/Will/.profile.
I tried placing the following line at the end of another existing file, /etc/pam.d/login.

Code: Select all

session optional pam_exec.so /bin/bash /usr/local/bin/connect-AIS0F.sh 
I created a new file, /home/me/.bash_login, containing only this line:

Code: Select all

bash /usr/local/bin/connect-AIS0F.sh 
I created an entry in XFCE's GUI tool under Settings:Session and Startup, in the Application Autostart tab that provided the fully qualified path to the script.

None of these attempts worked at all. None of them created the “touched” file BTC1.

What am I missing / doing wrong?
Thanks much for your ideas.

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: [Bash] Run script on Resume or on Login - breaking my head

#2 Post by Dai_trying »

Have you tried using cron? there are many examples on the net, here is one.

https://phoenixnap.com/kb/crontab-reboot

Wm. A. Weasel
Posts: 31
Joined: 2014-11-22 02:59
Location: Wisconsin USA
Has thanked: 7 times

Re: [Bash] Run script on Resume or on Login - breaking my head

#3 Post by Wm. A. Weasel »

Thanks, Dai_trying. No, I haven't tried cron, because if I understand right, cron can run a script on bootup, but it doesn't have a way to run something upon login (or resume). To amplify a bit, I rarely shut the computer down. I suspend it when it's not in use. Then when I open the lid it resumes, and I have to log in. If I could get the script to run on either of those events, that would achieve my goal.

I suppose I could run the script every 30 seconds or something, but that seems very inelegant when it only needs to run once each time I log in.

BTW, I didn't respond in a timely way because I was expecting the forum to email me when someone answered. I'll have to check my settings here.

reinob
Posts: 1196
Joined: 2014-06-30 11:42
Has thanked: 99 times
Been thanked: 47 times

Re: [Bash] Run script on Resume or on Login - breaking my head

#4 Post by reinob »

Should the script run as root or as a (specific) user?
If you want it to run when waking up, the best is to make a systemd service like:

Code: Select all

Unit]
Description=On wake-up
After=sleep.target

[Service]
User=your_user
Type=simple
ExecStart=/usr/local/bin/on_wakeup.sh

[Install]
WantedBy=sleep.target
This will run the "on_wakeup.sh" script, as your_user, when waking up.

Wm. A. Weasel
Posts: 31
Joined: 2014-11-22 02:59
Location: Wisconsin USA
Has thanked: 7 times

Re: [Bash] Run script on Resume or on Login - breaking my head

#5 Post by Wm. A. Weasel »

Thanks, reinob. The script can be run by me or by root - either is OK. Your advice looks good - systemd service is new to me, so I'll need to learn something about that. I'll research it a bit and report back. It will probably be after a few days.

Post Reply