I have a bash script that I've written that works if I execute from the terminal, but I want to be able to have this script ran when I login to my system. I have seen other questions similar to this, but the proposed solutions did not work for me. I tried adding the script path to my ~/.profile and the script still is not being ran. Anyone have an example or documentation on how to do this? Side note I am using Unix
-
2Need more details. Do you also have a `~/.bash_profile`? (If so, it'll be used in favor to `~/.profile`). *Exactly* how are you executing the script from a terminal? (Are you naming an interpreter in that case?); *Exactly* how are you testing, and in what precise scenario? (`~/.profile` is only run for login shells; to run code for other shells you need `~/.bashrc` or similar instead -- whether opening a new window in an existing GUI session runs a login shell or not varies between operating system vendors, and you haven't specified what OS you're on). – Charles Duffy Dec 12 '16 at 22:02
-
2Beyond that, inasmuch as this is a question about configuring your OS rather than developing software, it would be a better fit for SuperUser or Unix SE than StackOverflow. (There will be numerous duplicates on all three sites, however). – Charles Duffy Dec 12 '16 at 22:03
-
Examples of duplicate questions: http://stackoverflow.com/questions/97137/how-do-you-run-a-script-on-login-in-nix; http://stackoverflow.com/questions/9674176/run-a-bash-script-automatically-upon-login; http://stackoverflow.com/questions/10797150/how-to-run-a-script-after-user-login-authentication-in-linux; etc. – Charles Duffy Dec 12 '16 at 22:04
-
@Ninek: Could it be, that your bash is NOT executed as a login shell? You can verify this by typing `shopt -p` and see whether you have the option *login_shell* set. – user1934428 Dec 13 '16 at 06:23
3 Answers
If you already had a ~/.profile file it should be fine to just add it there. Otherwise look for a ~/.bash_profile and add the line there.
Did you make sure that your file is executable? Otherwise it will not work. Here is an example code (make sure to adapt to your needs):
echo "echo 'foo'" > /tmp/execute_me
chmod u+x /tmp/execute_me
echo "/tmp/execute_me" >> ~/.profile
login from another console (for safety), and you should see "foo" printed in your console somewhere.
If you want your script to be executed whenever a shell is used (even not interactive, you should add the code to the ~/.bashrc, read this for details: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work)
- 3,607
- 26
- 31
- 460
- 3
- 12
-
You also need to execute the **chmod** command, as above, only once after **.bashrc** has been created. – Arif Burhan Dec 14 '16 at 00:38
-
Thanks @Iarwa1n, thats exactly what I needed to do, but in my case since my current shell is zsh I needed to add the script to my .zshrc, and I just added echo `script_name.sh` and it works now! – Ninek Dec 14 '16 at 17:49
I don't if your system supports it, but calling scripts (as root or as a common user , via su - user -c "command") from /etc/rc.local works great on my "nix" system. You might want to add some delay and possibly pass the display variable as well. Example of rc.local entry:
sleep 20 && bash -c "env DISPLAY=:0.0 nohup /folder/script &"
- 2,033
- 3
- 15
- 18