LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux Mint (https://www.linuxquestions.org/questions/linux-mint-84/)
-   -   Starting script on boot as root (https://www.linuxquestions.org/questions/linux-mint-84/starting-script-on-boot-as-root-4175593537/)

lamic 11-14-2016 04:25 PM

Starting script on boot as root
 
Hello,
I'm trying to start a script with root permissions upon boot, but everything i tried so far isn't working.
First, I tried using crontab with @reboot option. Then i tried using /etc/init.d technique, but I'm not seeing the process running.

To give you little more information about the specific steps:

1) Created script called test.sh
Code:

#! /bin/bash
i=$1
while [ $i lt 100 ]
do
echo "Script running"
i=$[$i +1]
sleep 2
done

2) sudo chmod +x test.sh
3) Created startup.sh under /etc/init.d/
Code:

#! /bin/bash
nohup /home/user/Documents/test.sh 40 /dev/null 2$>1 &

4) chmod +x startup.sh
5) Added symlink
Code:

sudo ln -s /etc/init.d/startup.sh /etc/rc0.d/
Any suggestions would be highly appreciated.

michaelk 11-14-2016 05:18 PM

If you posted an exact copy of your script it contains errors and will not run.

Quote:

while [ $i lt 100 ]
Should be
while [[ $i -lt 100 ]]

The easiest way to get your script running is using cron or rc.local in my opinion.

What version of Mint are you running?

lamic 11-15-2016 08:24 AM

Yes, you are right! It was a typo, i have no issue with the script it self, but still trying to figure out how to start the script at boot with root privileges.

pan64 11-15-2016 08:29 AM

why do you think it was not running?

michaelk 11-15-2016 08:33 AM

Did you use sudo to add your job to roots crontab?

sudo crontab -e

lamic 11-15-2016 09:25 AM

Yes, i ran:
Code:

sudo crontab -e
And added
Code:

@reboot root /home/mist/Documents/test.sh 40 &
Note: I ran crontab under mist user using sudo .

michaelk 11-15-2016 09:35 AM

You only need to specify the user (i.e root) when adding a system cron job in the /etc/cron directories.

Try
sudo crontab -e
@reboot /home/mist/Documents/test.sh 40 &

pan64 11-15-2016 09:40 AM

still did not explain how did you check if that process was running?

lamic 11-15-2016 10:03 AM

I just tried that and no luck so far.

michaelk 11-15-2016 10:06 AM

No luck that it isn't running or not running as root?

lamic 11-15-2016 10:09 AM

@michaelk
The script is not even starting. I read somewhere(forgot where) that @reboot doesn't work anymore.

@pan64
After reboot, i run ps -ef | grep test

jpollard 11-15-2016 10:18 AM

The @reboot works - but you need to look at the logs to see what happened.

One of the potential issues depends on whether you are using systemd or not, and how /home or /home/mist are mounted.

In some cases the cron service starts before the network is ready... and that could mean that NFS mounts haven't been done yet. What I'm not sure of is whether even all the local mounts have been done yet. I think they are supposed to have been completed, but don't know for sure.

lamic 11-15-2016 10:36 AM

@jpollard
Code:

sudo stat /proc/1/exe
File: '/proc/1/exe' -> '/lib/systemd/systemd

Code:

grep CRON /var/log/syslog | grep "Nov 15"
Nov 15 08:17:01 mist-vm CRON[3310]: (root) CMD (  cd / && run-parts --report /etc/cron.hourly)
Nov 15 09:17:01 mist-vm CRON[3472]: (root) CMD (  cd / && run-parts --report /etc/cron.hourly)
Nov 15 10:17:01 mist-vm CRON[2311]: (root) CMD (  cd / && run-parts --report /etc/cron.hourly


michaelk 11-15-2016 11:27 AM

I don't have anything running Mint at the moment but @reboot does work on my systems that do use systemd.

You can verify that your entry is actually added by looking roots /var/spool/cron/crontabs file. Also make sure that you have a line feed character at the end of the entry. Go back and edit the crontab, move the cursor to the end of the line and press the enter key. Save crontab.

lamic 11-15-2016 12:51 PM

@michaelk
Code:

mist-vm crontabs # pwd
/var/spool/cron/crontabs
mist-vm crontabs # ls
root
mist-vm crontabs # cat root
@reboot /home/mist/Documents/test.sh 40 &

Also, i was trying to use another method with rc.local, and I added:
Code:

sh /home/mist/Documents/test.sh 40 &


All times are GMT -5. The time now is 11:27 AM.