Score:1

Run command at reboot

bo flag

I would like to preserve the battery on my laptop by limiting how much it gets charged.

There is a file (/sys/devices/platform/lg-laptop/battery_care_limit) with a value of 100. I can run command

echo '80' | sudo tee /sys/devices/platform/lg-laptop/battery_care_limit

I have to run this command at every reboot.

I have tried adding it via crontab -e but it doesn't work.

How do I get this command to run automatically at every reboot?

Additional information posted as an answer

thanks for the reply.

This works:

@reboot echo 80 > /home/myuser/test.txt

Tried and failed all the following:

@reboot echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit
@reboot sudo echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit
@reboot sleep 60 && echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit
@reboot sleep 60 && sudo echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit

At least the command is being attempted. journalctl -b | grep echo returns:

CRON[1041]: (myuser) CMD (sudo echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit)
CRON[1045]: (myuser) CMD (sleep 60 && sudo echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit)
PonJar avatar
in flag
Converting something that works in a terminal to something that works in cron can be problematic. You haven’t provided any information about the errors that are arising. You could try something like `echo 80 > /home/myuser/test.txt` in your cron job to confirm cron is working. You don’t need the tee command because cron doesn’t output to STDOUT. You could try `sudo echo 80 > /sys/devices/platform/lg-laptop/battery_care_limit` and trouble shoot from there
Raffa avatar
jp flag
@PonJar Redirection in the shell happen before the execution of the command so not affected by `sudo` and nothing will be written to the file after `>` that way as it need elevated permissions ... Please see: https://www.gnu.org/software/bash/manual/html_node/Redirections.html
Elder Geek avatar
cn flag
another option can be found [here](https://askubuntu.com/questions/1151080/how-do-i-run-a-script-as-sudo-at-boot-time-on-ubuntu-18-04-server)
Elder Geek avatar
cn flag
Does this answer your question? [How do I run a script as sudo at boot time on Ubuntu 18.04 Server?](https://askubuntu.com/questions/1151080/how-do-i-run-a-script-as-sudo-at-boot-time-on-ubuntu-18-04-server)
Score:1
jp flag

sudo has no significance in crontab entries.

If you need to run a cron job with elevated permissions(like sudo does), then add an entry for it to the root's crontab using:

sudo crontab -e

Which runs the jobs as root instead of just:

crontab -e

Which runs the job as your user.

elwood avatar
bo flag
Thanks Raffa, That worked :) Which of these two is the best practice for delaying the cron job? `@reboot sleep 10; echo 80 > ...` or `@reboot sleep 10 && echo 80 > ...`
Raffa avatar
jp flag
Both might work but I would go with `@reboot sleep 10 && echo 80 > ...` as this will execute the second command only if the first command succeeds ... And please read about how this site works [here](https://askubuntu.com/help) ... You are adding answers that should be either edits to your question or comments to other people's answers ... People here like things to be tidy and you're making them unhappy :-D ... So please leave only your question and delete your answers and accept my answer if it works for you so future readers will find the right solution easily.
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.