Cant execute script as root

I have a test script (well it just the very first part of the script i want to use)

❱cat bash/btrfs-to-sda-backup.sh
#!/bin/bash

# Variables
SNAPSHOT_DIR="/mnt/snapshots"
BACKUP_MOUNT="/mnt/target"
BACKUP_SNAPSHOTS="$BACKUP_MOUNT/snapshots"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")


# Check if the backup mount point is already mounted
if ! /usr/bin/mountpoint -q "$BACKUP_MOUNT"; then
  echo "$BACKUP_MOUNT is not mounted. Mounting now..."
  /usr/bin/mount /dev/sda3 "$BACKUP_MOUNT"
else
  echo "$BACKUP_MOUNT is already mounted."
fi
/usr/bin/umount "$BACKUP_MOUNT"
exit

I have adjusted visudo:

❱sudo -l
Matching Defaults entries for greg on greg-optiplex7050:
    timestamp_timeout=120

User greg may run the following commands on greg-optiplex7050:
    (ALL) NOPASSWD: /home/greg/bash/btrfs-to-sda-backup.sh
    (ALL) ALL

It runs fine manually

❱sudo /home/greg/bash/btrfs-to-sda-backup.sh
/mnt/target is not mounted. Mounting now...

Iv added it to crontab

❱crontab -l
*/3 * * * * sudo /home/greg/bash/btrfs-to-sda-backup.sh >> /home/greg/btrfs-backup.log 2>&1

The permissions are as they should be, owner and group “greg:greg” executable flags set

❱ls -al /home/greg/bash/btrfs-to-sda-backup.sh
.rwxr-xr-x 2.6k greg 13 Jun 20:27  /home/greg/bash/btrfs-to-sda-backup.sh

but it just keeps asking for a password?

❱cat btrfs-backup.log
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required

Iv had chatGPT4o running around in circles on this, i think i pissed it off ^^

I cant figure why it keeps asking for a password and wont run?

Why do you adding a script to a normal users crontab if it needs to be executed with elevated permissions? Why not adding this script to roots personal crontab? No sudo required to run it.

3 Likes

hmm, well i never thought of that. iv always done it this way.

I was going to use vorta to execute the script after it had finished its backups to the NAS by just adding the script to it’s “Post-backup” shell command option. Ii really dont want to involve cron at all.

I suppose i could do it via root crontab. But why wont this work, its works like this before?

Did it really worked before? I never used cron in recent years, but I don’t remember that something like this worked. I always started the script as a normal user and added the sudo command in front the programs that need elevated permissions. Of course this would mean you need to add /usr/bin/mount and the umount command to sudoers wit NOPASSWD. But adding such a important command to it, might be considered a security risk.

Don’t ask ChatGPT - it doesn’t “know” squat - just repeats what gets repeated on the interwebs.
(and is woke biased, too … which is “good to know” IMO, but not important here)

I can’t believe that you have always done it this way - since it obviously asks for a password and this would never have been different before.

2 Likes

well not this exact script but a root run script yes it did work.

Ok so how would i add it to vortas post-backup script? which was where i initial started out failing before moving to crontab to test.

That’s the proper way. Better yet, use a systemd service.

Then why are you? Run it the way you want to run it.

1 Like

Probably along the same lines and definable as per the previous statement.

Its a bit amusing the word is so misused and misunderstood.

Not because its ironic - quite the contrary. See the term had a history of use in hiphop and similar communities well before it entered mainstream consciousness. It wasnt necessarily political either.
It largely stood as an antonym for asleep, ie: ignorant. Like “dont sleep, read a book, get woke”.

The fact that its somehow gained notoriety while still being widely confused … is just so perfect for slang of any flavor. “Are you kids hitting the mary jane cigarettes?”. But extra so for this particular term as it is supposed to be the opposite of uninformed.

Thank you for coming to my ted talk.

3 Likes

Please no, let’s not get into the [neo-marxist social contagion]., i beg of you :stuck_out_tongue:

I did add to my comment (to clarify it a bit) moments after you quoted it.

Hm?
I’m often extremely slow to pick up on irony - but I think I got it this time. :smiley:

right - that will have been it right here :grinning:

My preferred approach is to use systemd

If the service is run as root I have a folder

/etc/systemd/scripts

Then I create a systemd service in

/etc/systemd/system/<myservice.service>

If the service is to be run at specific intervals I create a timer

/etc/systemd/system/<myservice.timer>

Then I enable and start the timer

systemctl enable --now <myservice.timer>

For a practical implementation of a user script/service/timer construct - you could look at [root tip] [Utility Script] Charger Notifier - battery state

Once you understand the concept it would be fairly easy to adapt to a systemwide service.

practical appliance

See-> systemd/Timers - ArchWiki

Switch to root context

su -l root

Create a folder to hold your script (executed by service)

mkdir -p /etc/systemd/scripts

Copy your script to the folder

cp btrfs-to-sda-backup.sh /etc/systemd/scripts

Switch to /etc/systemd/system folder

cd /etc/systemd/system

Create a service file

cat << EOF > /etc/systemd/system/btrfs-to-sda-backup.service
[Unit]
Description=Backup to SDA

[Service]
Type=oneshot
ExecStart=/etc/systemd/scripts/btrfs-to-sda-backup.service

[Install]
WantedBy=multi-user.target
EOF

Create the timer

cat << EOF > /etc/systemd/system/btrfs-to-sda-backup.timer
[Unit]
Description=Backup to SDA

[Timer]
OnBootSec=3h
OnUnitActiveSec=3h

[Install]
WantedBy=timers.target
EOF

Activate the timer

systemctl enable --now system/btrfs-to-sda-backup.timer
3 Likes

Thx, I have used systemd timers before but i just wanted to put the execution under vortas control.

It just seems better to me to have all the backup timings in one place.

It works from roots crontab, and im sure it would work from systemd. I find systemd a massive headache especially to just fire a single script once every few hours, massive overkill and head scratching.

But is there not a way to do it from my under my user then? I know i have done it before. If not i shall just leave it in roots cron.

You can work around this by creating two more units.

  • mount unit - define the mountpoint (do no enable)
  • automount unit - mount when mountpoint is accessed

This will simplify your script - remove the mount check - just access the path with the script - mounting is done by the automount and is unmounted again after predefined idle time.

This is error prone - systemd does not guarantee that a given device is always assigned the same device id, in this case sda.

You have other safer options - like label and uuid - where uuid is the safest identifier

/dev/disk/by-label/<label>
/dev/disk/by-uuid/<uuid>

So the mounts are just the very first of the many root commands that I’m actually using. This is just a test script.

O and did i mention i hate systemd, well i do. its to complex and they have even removed the gui that made it a little easier.

I shall stick with root cron.