0

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
Kulfy
  • 17,416
  • 26
  • 64
  • 103
Dave P
  • 1
  • Which Ubuntu are you using? – user68186 Mar 20 '21 at 14:00
  • 1
    In this case use `sudo crontab -e` to schedule a cron job at root. – user68186 Mar 20 '21 at 14:09
  • 1
    "which I'll schedule in cron" then why do you use sudo? No need for that: cron runs as root unless you use /etc/crontab with a user. Another issue: vcgencmd needs an absolute path in front of it. Thirdly: the ~ is NOT going to point to your user but to /root/, Also here: use an absolute path – Rinzwind Mar 20 '21 at 16:10
  • Thanks for your feedback. Im using Cron by typing crontab -e and I'm not navigating to the root first so I guess that's at user level if that's what you mean? Please can you explain what you mean by absolute as Im just entering the path for my Desktop when I right click on the Desktop and goto view in terminal. For the last comment do I just remove the ~ ? – Dave P Mar 21 '21 at 16:34
  • Search "crontab -e" in this site, and read some of the answers. It is not the matter of "navigating to root first". When you use `sudo crontab -e` the command you put in is executed by not you DaveP, but a different user whose name is `root`. Since `root` will execute script, you don't need `sudo 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:36
  • However, if the user called `root` executes the command, or you schedule it in cron on behalf of `root` by using `sudo crontab -e`, then the "home" folder for `root` is `/root/`. Now you don't want your file `readings.txt` in `/root/Desktop/` folder. So you need to put the absolute path for the files `readings.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
  • Thanks again, i'll give it a go in the morning! – Dave P Mar 23 '21 at 22:20

0 Answers0