Running script as user with root permissions

Solution 1:

One way would be to create a systemd unit.
All the possible options you can find here: www.freedesktop.org/.../systemd.unit.html but I am going to present simple example:

1.Lets create a bash script which will write the date and time of each system bootup to the file: /root/file
Only root user has permissions to write and edit files in /root directory.
I am going to switch user to the root with sudo su -
Lets create a script /root/timelog.sh and make it executable chmod +x /root/timelog.sh

   #!/bin/bash

   if ! [ /root/file ]; then touch /root/file; fi 
   echo $(date) >> /root/file

2.Now we need the System Unit file: /etc/systemd/system/timelog.service

[Unit]
Description=timelog.service

[Service]
Type=oneshot
ExecStart=/root/timelog.sh

[Install]
WantedBy=multi-user.target

3.Last task is to make sure it will start with the system:

systemctl enable timelog