LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-29-2012, 05:33 AM   #1
andperry
LQ Newbie
 
Registered: Mar 2012
Location: Wirral, UK
Distribution: Ubuntu 22.04
Posts: 8

Rep: Reputation: 0
Running a script at startup


I'm trying to automatically run a script at startup (preferably once at boot time, but it would suffice to be able do it each time the user logs in). This is something I've done with ease many times in Windows and even in the past with older operating systems. Why is it proving so difficult with Linux? Using Ubuntu 11.10 - have tried two approaches but so far to no avail:-

1. Using cron, but when doing a crontab edit the system is just not saving the changes.

2. Adding a call to or inclusion of the script in ~/.profile. The latter exists by default in the Ubuntu installation whilst the files ~/.bash_profile and ~/.bash_login are absent. This should mean that .profile is executed at startup time, but there is no evidence that the required script is being run.

Can anyone please help?

Many thanks,

Andrew.
 
Old 04-29-2012, 05:42 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
1. crop will run scripts at fixed times. It does not try to run any script at startup at all.
2. .profile/.bash_profile scripts are sourced at time of log of the user so no it won't gets run at the startup.

What you should do is to add the script to /etc/rc.d. Check the dir for more information.
 
Old 04-29-2012, 06:27 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Both Dillon and Vixie cron systems allow the use of @reboot in the crontab time field but adding to the boot scripts mechanism is a more natural way to achieve the same thing.

Interesting that ~/.profile does not seem to work. What is the evidence? How are you logging on?

Alternatively, you could hook into the graphical desktop session startup mechanism.
 
Old 04-29-2012, 07:33 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
.profile is executed as a part of the login process and you will get no prompt during the execution. You can write logfile in your script and you will see if it was executed, also you can run it in the background.
 
Old 04-30-2012, 03:44 AM   #5
andperry
LQ Newbie
 
Registered: Mar 2012
Location: Wirral, UK
Distribution: Ubuntu 22.04
Posts: 8

Original Poster
Rep: Reputation: 0
Many thanks for all the hints and tips - they helped me to look in the right direction for an answer. I got there in the end but not without more hurdles - I'll share my experience for the benefit of anyone else with a similar problem.

1. Basically all that was necessary to make something run at boot time was to add the appropriate lines of script on to the end of the /etc/init.d/rc.local script.

2. There was a problem with the script that I was trying to call anyway. I don't know whether a boot time script is validated before it is run - maybe it is in view of what happened. The script contained a read statement for terminal input (obviously not appropriate in a boot time script), although the read statement should not have actually been invoked with the paramaters that were being passed. Anyway, I managed to get the script to run by taking out the read statement and associated conditions.

3. Having modified the script however, I was getting a 'hiccup' during the initial login. As I started to type in my password, the system jumped to a black screen then back to the login screen with the password field cleared. On re-entering the password everthing was fine. Maybe this was to do with the fact that the script is performing quite a complex function - it actually calls a PHP script running in command line mode, which accesses a MySQL database and writes to a number of files.

4. The final solution was for the boot time script to write a dummy file just to say "I've booted". The .profile script then looks for that file and if it finds it, deletes it then calls a script to do the real job. The only thing I've discovered is that the system has to be set to do a manual login - an auto login at boot time seems to skip the operations in .profile.

5. It might look convoluted but it finally works and I'm happy.
 
Old 04-30-2012, 04:43 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks for sharing your findings

Threads can be marked SOLVED via the Thread Tools menu.
 
  


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
Running Script on Startup jCash Linux - Newbie 3 05-20-2007 08:45 PM
Running a bash script at startup linker3000 Linux - Software 2 05-17-2007 10:00 PM
running .pl script on startup knowram Other *NIX 2 03-09-2007 09:50 PM
Running Perl Script upon startup andy7t Linux - Software 1 04-20-2005 04:43 PM
Running a script at systsem startup logo Linux - Newbie 1 09-21-2004 02:29 PM

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

All times are GMT -5. The time now is 01:08 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
Open Source Consulting | Domain Registration