How to autorun sudo command after startup

So… I have to restart bluetooth every time I start up my laptop because of an update that, at some point this past year, permanently(?) broke bluetooth. What I’d like to do is automate this restart. I’ve tried the various approved methods, and none have worked. I want to run the following command automatically 30 seconds, let’s say, after arriving at my DE. (I could add sleep 30.)

echo password | sudo -S systemctl restart bluetooth

Security protocols be damned.

On Plasma, I would simply add this to startup, but Gnome has stripped this functionality from their settings app [don’t me started :angry:]. So where does one automate a sudo command, in Gnome, after startup?

@millstonepoet autostart is still there in the Tweak Tool?

Bluetooth works fine here, dongle and on a couple of laptops? Perhaps a ‘connection’ issue with the device?

Perhaps explain the issue your having in some more detail, bluetooth hardware, device your wanting to connect etc.

If you know how to run this command after startup, that’s what would be the most help. Thanks.

Edit: // autostart is still there in the Tweak Tool?//

Only for pre-selected apps. One can’t enter in commands anymore.

A 30 second google search would find how to configure sudo to not ask for a password.

You should probably do that if you really don’t want to fix the real problem. It wouldn’t be a great support forum if everyone blindly helped someone just ignore their problem.

1 Like

Right.

A.) I didn’t ask for “how to configure sudo to not ask for a password”.

B.) If you don’t know the answer to the question, move on, but don’t posture over what you think the question should have been. If you know where to enter this command so that it runs during or after startup, that’s all I need to know. And my thanks for that.

A cron job using @reboot might be what you’re looking for. You can also introduce a delay by prefixing the command used in the cron job with sleep [duration] && [command]

I agree with Malcolm that it would be better to diagnose why bluetooth is behaving the way it is on your system, but I understand that sometimes a temporary fix is needed until one has time to actually do that troubleshooting.

I would like to remind everyone that being kind in your responses is generally going to be appreciated.

3 Likes

Thanks.

That almost did the trick. I entered into crontab:

sleep 10 && systemctl restart bluetooth

(This command/workaround, issued on the command line, always fixes bluetooth.)

When I rebooted, bluetooth automatically connected to my speaker and correctly played through the speaker. (The buggy behavior had it connecting, but still playing through the laptop’s speaker.) The only weird behavior now is that it’s running the command every ten seconds. That is, every 10 seconds bluetooth is restarted (unless I’m playing audio).

I wonder if it’s running crontab after ever systemctl command?

Is the bluetooth service failing or why do you have to restart it?

Maybe you could solve the problem by editing the /usr/lib/systemd/system/bluetooth.service

E.g.by setting Restart=on-failure in the [Service]-section.

Or depending on what the problem is you might also get around it by putting your sleep time in front of the ExecStart-command in the .service-file.

Another idea would be to switch WantedBy to graphical.target instead of bluetooth.target, as that should be reached later I think.

Another but less elegant solution would be to write your own service that is Wanted By e.g. bluetooth.target or graphical.target and just launches sleep 30 && systemctl restart bluetooth

AFAIK systemd services use anyways root privileges. That way you wouldn’t need to put your password as clear text in any command.

That are just some inputs you might want to play around with.

Edit: didn’t read your replies on the cronjob before writing that

Trying to figure out why bluetooth isn’t working is a hamster wheel I’ve already run. This issue has been widely reported and I’ve already spent hours on it. I’m done trying to sort it out. (Don’t ask me to clarify.) The only solution (found elsewhere) is to restart the service.

And now this too seems to be broken.

I’ve removed the job from crontab and the behavior is still the same. As soon as I stop playing audio, the bluetooth service insists on restarting, repeatedly, until it successfully borks the connection to the bluetooth speaker. Then it’s happy. Once again, the audio only plays through the laptop. It’s flipping bizarre behavior. I’m wondering now if the last TW update (done just this morning) is the new culprit. :face_with_symbols_over_mouth:

Seems like bluez got updated 10 days ago. Depending how often you run updates that could be a reason for changed behaviour after your last update.

Does that mean that restarting the bluetooth.service is no longer a solution/workaround for you?

I’m using snapper to undo the update. Going to take a while. I’ll retest once it’s done. But if the update is the culprit, then I guess I won’t be updating TW for the foreseeable future (yeah, I know) being that there’s no other solution/workaround.

It shouldn’t be running after every systemctl command, but make sure the line is:

@reboot sleep 10 && systemctl restart Bluetooth

You didn’t indicate that you included @reboot in the crontab - that’s needed.

1 Like

Yeah. Thanks. Confirmed and done. I no longer think that cron is to blame. The behavior remained even after snapper removed cron. My current bet is on the update. I’m rolling it back right now.

Also, this bug seems to be hardware specific —> MS Surface GO.

I’m also running TW on a Lenovo T470s and not having the same issues. The other difference could be that I’m running Plasma on my Lenovo and Gnome on the GO. I know it’s not the Kernel because the Kernel has been locked on the MS Surface since before these bluetooth issues cropped up.

Did you experience also problems with wifi or just with bluetooth?

Just with bluetooth. Rollback is complete but the new symptoms persist. Cron is completely removed, so it’s not cron. I’m mystified. It’s a mystery. Am deciding whether to reinstall with gecko or to try spiral linux (same dev). Since getting the latest/greatest isn’t really all that important on the MS GO—Debian might be the way to fly. I can’t even run the latest kernels on the GO because the kernel devs borked [who knows what], preventing the GO from properly shutting down, so what’s the point to running TW on the hardware? Feh. Just busy work is all.

Someone patched a kernel especially for MS Surface systems.
Here is the link to the project: Installation and Setup · linux-surface/linux-surface Wiki · GitHub

The maintainer also has an OBS-repo for openSUSE Tumbleweed.

Just in case you didn’t try that yet and are willing to.

I just looked into tlp for a different reasaon and realized by checking the settings that unwanted bluetooth behaviour could be caused by energy/battery management.

Maybe you could stop tlp and/or power-profiles-daemon if you have it running for testing purposes.

Sorry for the radio silence. Last night I finally said: Screw it, and went distro hopping. First I tried Spiral Linux/Gnome. I like that dev. Only problem was, there’s a bug that prevents GDM from listing Wayland as an option and I couldn’t, for the life of me, figure out how to start Gnome in Wayland. Deleted 61-gdm.rules and conf files. Commented out all kinds of GDM nonsense but nothing. Then I installed Ubuntu. But then remembered that Ubuntu has this &#$! nasty habit of always overwriting my board.bin WIFI files (necessary for my MS GO laptop), killing WIFI, and decided, why not let’s try TW again, and this time installed Gecko (cause I like the dev), and I’m writing this comment right now, listening to spotify over my bluetooth speaker. Flawless. I ran into the same (widely reported) bug that started Gnome in X11 and refused to give me an option for Wayland in GDM. Then. Finally. It dawned on me. Install LightDM. Et voila! Wayland! Everything works. Like a greased pig. And just as fast. All kinds of problems have disappeared, and I’m happy to be using TW—cause I really like Opensuse. I don’t know why I was having so many problems with my old TW install. Could have been problems caused by me (although I don’t much fool with OS-level files or settings) or cumulative TW updates. So. Anyway. I’m running Gecko and, so far, I’m not having being troubled by the previous Kernel issues.