I've written a short bash script to record the temperature of my Pi which I'll schedule in cron to run every 15 mins and output to a file on my desktop called readings.txt
The problem is the command needs sudo
privileges to run. So my question is how do I enable automatic entry of the password into the command within the script?
#!/bin/bash
sudo vcgencmd measure_temp >> ~/Desktop/readings.txt
sudo crontab -e
to schedule a cron job at root. – user68186 Mar 20 '21 at 14:09sudo crontab -e
the command you put in is executed by not you DaveP, but a different user whose name isroot
. Sinceroot
will execute script, you don't needsudo vcgencmd
inside your script. Now~
refers to the home folder of the user. If the user is you, DaveP then your home folder is at/home/davep/
. Therefore the absolute path to that~/Desktop/
folder is/home/davep/Desktop/
. – user68186 Mar 22 '21 at 20:36root
executes the command, or you schedule it in cron on behalf ofroot
by usingsudo crontab -e
, then the "home" folder forroot
is/root/
. Now you don't want your filereadings.txt
in/root/Desktop/
folder. So you need to put the absolute path for the filesreadings.txt
as/home/davep/Desktop/readings.txt
. You may also need to put the absolute path for/path/to/vcgencmd
as cron environment has only a few paths set by default. – user68186 Mar 22 '21 at 20:37