0

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

4
  • 2
    Need 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). Commented Dec 12, 2016 at 22:02
  • 2
    Beyond 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). Commented Dec 12, 2016 at 22:03
  • Examples of duplicate questions: stackoverflow.com/questions/97137/…; stackoverflow.com/questions/9674176/…; stackoverflow.com/questions/10797150/…; etc. Commented Dec 12, 2016 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. Commented Dec 13, 2016 at 6:23

3 Answers 3

0

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)

Sign up to request clarification or add additional context in comments.

2 Comments

You also need to execute the chmod command, as above, only once after .bashrc has been created.
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!
0

The script should be called .profile and in your scripts directory.

Comments

0

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 &"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.