LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-19-2015, 04:38 PM   #1
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Rep: Reputation: Disabled
Automatically starting a python script on startup after reboot...


I am running a headless Linux 17 media/NAS server. I have setup and tested the plexconnect script and it works. My only issue is that I can't figure out how to get the script to automatically run on startup. I tried using the GUI, but it doesn't seem to work.

To get it to work, I have to open terminal and navigate to the directory:

/usr/local/lib/PlexConnect/

Then run: sudo ./PlexConnect.py

The window has to remain open in order to keep running (which isn't an issue as it's headless). I'd just like to not have to login and start it each time the power goes out, or is rebooted.

I tried editing the crontab file and adding the following variations with no luck:
@reboot python /usr/local/lib/PlexConnect/PlexConnect.py
@reboot /usr/local/lib/PlexConnect/PlexConnect.py

Neither has worked. Does anyone know what I'm doing wrong?

Thanks!
 
Old 01-19-2015, 05:07 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,466
Blog Entries: 11

Rep: Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375
try
Code:
python /usr/local/lib/PlexConnect/PlexConnect.py
in /etc/rc.local
 
Old 01-19-2015, 05:15 PM   #3
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
try
Code:
python /usr/local/lib/PlexConnect/PlexConnect.py
in /etc/rc.local
Just tried it and it still doesn't appear to be starting.
 
Old 01-19-2015, 07:03 PM   #4
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Still no luck

I also tried putting the & symbol at the end of the string as well in the rc.local file ) still no go. This is how my rc.local file currently looks:
Code:
(python /usr/local/lib/PlexConnect/PlexConnect.py) &

exit 0
Any clues as to why this script wont run on startup?
 
Old 01-20-2015, 08:04 AM   #5
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,466
Blog Entries: 11

Rep: Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375
What are the permisssions on /etc/rc.local?

If you run
Code:
sh /etc/rc.local
does the python script run?
 
Old 01-20-2015, 01:52 PM   #6
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
What are the permisssions on /etc/rc.local?

If you run
Code:
sh /etc/rc.local
does the python script run?
Thank you for your reply. The script does run, but it doesn't work. For example if I run:
Code:
sudo /usr/local/lib/PlexConnect/PlexConnect.py
The program opens and runs. My rc.local file currently contains:
Code:
(python /usr/local/lib/PlexConnect/PlexConnect.py)&
When I run:
Code:
sh /etc/rc.local
the file starts to run but stops with an error of:
Code:
Failed to create socket on UDP port 53: [Errno 13] Permission denied
I think your assumption of permission conflict is the problem. The problem is I don't know what to change.

Thanks again!
 
Old 01-20-2015, 02:42 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,466
Blog Entries: 11

Rep: Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375Reputation: 2375
Quote:
Originally Posted by Habitual View Post
What are the permisssions on /etc/rc.local?
Code:
ls -al /etc/rc.local
output please.
 
Old 01-20-2015, 03:11 PM   #8
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Code:
ls -al /etc/rc.local
output please.
output:
Code:
-rwxr-xr-x 1 root root 359 Jan 20 11:41 /etc/rc.local
 
Old 01-20-2015, 04:40 PM   #9
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Ok, I just ran the code again, and for some reason it now reads:

Code:
 4 /etc/rc.local
 
Old 01-20-2015, 05:02 PM   #10
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 823

Rep: Reputation: 237Reputation: 237Reputation: 237
As what user are you running this:
Code:
sh /etc/rc.local
I am asking because the working command line was prepended by sudo, so you should do that here, too.
Note that you can also use root's crontab to run something at boot with root privileges.
Code:
sudo crontab -e
And then, in the crontab, add the line
Code:
@reboot /usr/local/lib/PlexConnect/PlexConnect.py
at the bottom

Last edited by joe_2000; 01-20-2015 at 05:04 PM. Reason: typo
 
Old 01-20-2015, 05:10 PM   #11
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 823

Rep: Reputation: 237Reputation: 237Reputation: 237
Oh, sorry, just saw you tried the cron thing already. Could it be your script depends on a working network connection? Then you would have to make sure that networking is up before the script runs.
I am asking because the error message indicates that the script is opening a socket.
Depending on which init system you should be able to specify dependencies such dependencies.
Alternatively, you could maybe put your script into /etc/network/if-up.d/

Then it would run once the networking is up.
 
Old 01-20-2015, 05:28 PM   #12
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
Oh, sorry, just saw you tried the cron thing already. Could it be your script depends on a working network connection? Then you would have to make sure that networking is up before the script runs.
I am asking because the error message indicates that the script is opening a socket.
Depending on which init system you should be able to specify dependencies such dependencies.
Alternatively, you could maybe put your script into /etc/network/if-up.d/

Then it would run once the networking is up.
That's not a bad idea. I can give it a try. But when I run the rc.local file, I am doing so from a headless server, so the network is already running when I try and run it. The script does work though if I run it directly by means of sudo. When I try and run the rc.local file, it continually does the error. So I'm thinking it is a permissions or some sort of root problem that I can't figure out.
 
Old 01-21-2015, 02:16 PM   #13
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 823

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by win_the_day_go_ducks View Post
The script does work though if I run it directly by means of sudo. When I try and run the rc.local file, it continually does the error. So I'm thinking it is a permissions or some sort of root problem that I can't figure out.
I understood that, but from what you posted it did not become clear as what user you were running rc.local. Try
Code:
sudo sh /etc/rc.local
just to be sure...
 
Old 01-21-2015, 02:44 PM   #14
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
I understood that, but from what you posted it did not become clear as what user you were running rc.local. Try
Code:
sudo sh /etc/rc.local
just to be sure...
when running:

Code:
sudo sh /etc/rc.local
It does work correctly.
 
Old 01-21-2015, 02:45 PM   #15
win_the_day_go_ducks
LQ Newbie
 
Registered: Sep 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
I understood that, but from what you posted it did not become clear as what user you were running rc.local. Try
Code:
sudo sh /etc/rc.local
just to be sure...
when running:

Code:
sudo sh /etc/rc.local
It does work correctly. Running it without sudo does not.
 
  


Reply

Tags
autostart, python, reboot


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Automatically starting oracle 11g database on system startup mlnm Linux - Newbie 6 12-16-2012 06:24 PM
Starting MySQL automatically on Slackware 10 on system startup abtimoteo Slackware 2 09-24-2007 01:40 AM
starting service automatically on reboot msteiner Linux - Newbie 2 06-15-2007 11:13 AM
startup backup agent automatically when reboot ashley75 Linux - General 1 07-18-2005 03:39 PM
Starting a vncserver instance automatically at startup paul.nel Linux - General 6 01-08-2004 08:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:21 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration