LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running a script at startup (https://www.linuxquestions.org/questions/linux-newbie-8/running-a-script-at-startup-942380/)

andperry 04-29-2012 05:33 AM

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.

ozanbaba 04-29-2012 05:42 AM

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.

catkin 04-29-2012 06:27 AM

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.

pan64 04-29-2012 07:33 AM

.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.

andperry 04-30-2012 03:44 AM

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.

catkin 04-30-2012 04:43 AM

Thanks for sharing your findings :)

Threads can be marked SOLVED via the Thread Tools menu.


All times are GMT -5. The time now is 04:44 PM.