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
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)
2 Comments
script_name.sh and it works now!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 &"
~/.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? (~/.profileis only run for login shells; to run code for other shells you need~/.bashrcor 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).shopt -pand see whether you have the option login_shell set.