1

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)
muru
  • 197,895
  • 55
  • 485
  • 740
elwood
  • 13
  • 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 – PonJar Dec 31 '22 at 09:46
  • @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 – Raffa Dec 31 '22 at 13:19
  • another option can be found here – Elder Geek Jan 31 '23 at 23:40

1 Answers1

1

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.

Raffa
  • 32,237
  • 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 > ... – elwood Dec 31 '22 at 13:24
  • 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 ... 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. – Raffa Dec 31 '22 at 13:34