Score:1

How to run a command as root on login

bd flag

I want to run this command as root on login:

systemctl mask --now sleep.target suspend.target hibernate.target hybrid-sleep.target

I had some problems with the laptop randomly logging out, every 1 - 2 minutes before I applied this script. (This question is not asking to solve this problem)

I can make a code that allows me to unmask these on demand, and then logout, allowing me to save battery.

I have attempted, as the answer here told me to, to have it run via editing the ~/.profile file, but it then showed a black screen after login, and I had to revert that change in recovery mode before it allowed me to login again.

I just naively added the code like this, to the end of the file:

~/.profile:
...
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
systemctl mask --now sleep.target suspend.target hibernate.target hybrid-sleep.target

Is there another way I can run this script on login? Or did I do something wrong and how can I do it right?

It is a Samsung Galaxy Book 2, with Windows dual boot, Ubuntu 20.04 with the latest kernel(6.1.9) installed.

Previously, I used Linux Mint on an older Samsung machine, and it ran flawlessly.

sudodus avatar
jp flag
Try with `sudo crontab -e` and edit the command into the crontab file with a line starting with `@reboot`. See [this link](https://linuxhint.com/cron_jobs_complete_beginners_tutorial/) and other tutorials via the internet if you need more details. Please notice that you may need full path to the commands for example `/usr/bin/systemctl`
joonho park avatar
bd flag
I need one at login, not at boot. There doesn't seem to be a crontab setting for logins
sudodus avatar
jp flag
Sorry, I was not reading the question correctly. At login you can use 'autostart'. In order to avoid having to enter password, you can edit the sudo behaviour: `sudo visudo`. [This link](https://askubuntu.com/questions/1353879/lubuntu-20-04-run-a-script-at-login-which-executes-in-a-terminal-window/1354182#1354182) may help you make it work.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.