LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux Mint
User Name
Password
Linux Mint This forum is for the discussion of Linux Mint.

Notices


Reply
  Search this Thread
Old 11-14-2016, 04:25 PM   #1
lamic
Member
 
Registered: Jan 2013
Posts: 37

Rep: Reputation: Disabled
Question 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.
 
Old 11-14-2016, 05:18 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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?
 
Old 11-15-2016, 08:24 AM   #3
lamic
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-15-2016, 08:29 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,811

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
why do you think it was not running?
 
Old 11-15-2016, 08:33 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Did you use sudo to add your job to roots crontab?

sudo crontab -e
 
Old 11-15-2016, 09:25 AM   #6
lamic
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 11-15-2016, 09:35 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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 &
 
Old 11-15-2016, 09:40 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,811

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
still did not explain how did you check if that process was running?
 
Old 11-15-2016, 10:03 AM   #9
lamic
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
I just tried that and no luck so far.
 
Old 11-15-2016, 10:06 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
No luck that it isn't running or not running as root?
 
Old 11-15-2016, 10:09 AM   #11
lamic
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
@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
 
Old 11-15-2016, 10:18 AM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
Old 11-15-2016, 10:36 AM   #13
lamic
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
@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
 
Old 11-15-2016, 11:27 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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.
 
Old 11-15-2016, 12:51 PM   #15
lamic
Member
 
Registered: Jan 2013
Posts: 37

Original Poster
Rep: Reputation: Disabled
@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 &
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting Script on boot - what am I doing wrong? pazu Linux - General 4 02-19-2015 05:20 AM
starting a programme from boot script in Etch knobby67 Debian 10 02-05-2008 03:38 AM
Starting a daemon in an init.d bootup script without root being the owner BobTheSlob Linux - General 1 09-19-2007 11:19 AM
starting the script at boot time hudy_rob Mandriva 4 12-09-2004 10:29 AM
starting a script on boot jax8 Linux - Newbie 1 03-19-2004 10:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux Mint

All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration