3

I am hacking around with a component on a board that needs networking. To accomplish that I open a terminal at startup and I added a startup.sh script to the bottom of my ~/.bashrc. However, when I restart and then run a process check I notice 2 instances.

pi         841   826  0 07:17 tty1     00:00:00 /bin/bash --rcfile ./bin/startup.sh
pi         998   985  0 07:17 pts/0    00:00:00 /bin/bash --rcfile ./bin/startup.sh

Now if I remove the line from .bashrc and then add after startup and open a terminal I only get 1 instance. I disable the line in the bashrc before opening the other terminal to run ps so it shouldn't be that either (also seems to be confirmed by PID)

Why is this happening? Can I make it so only 1 is starting?

  • 3
    Using .bashrc to do anything other than configure the shell/environment is a bad practice that for whatever reason is virulent among pi users -- likely due lousy blogs spreading it like a virus. – goldilocks Nov 12 '17 at 15:33
  • Good point I was incorrectly assuming that .bash_profile ran before networking was set up. However, I switched it around and it still seems to work. So if I move it there it does seem to work better, – Jackie Nov 12 '17 at 15:39
  • 2
    You've been misled. Exploiting the autologin commonly used with the pi makes it seem like .profile or .bashrc are scripts executed by the system at boot. They aren't -- they are executed on a per user basis whenever a new shell is started, depending on context. If you are using the system interactively (i.e. via a keyboard, etc.), or logging in remotely, this could happen many times, and not only at boot. See INVOCATION in man bash. You've gotten around part of the issue by using --rcfile so that that file won't normally be used. – goldilocks Nov 12 '17 at 15:52
  • So where would you suggest? Although I understand those concerns no one should be logging into the box. Do I need to dig into something more basic in the os? – Jackie Nov 13 '17 at 1:51
2

If you’re looking to launch some program on boot, you should use a systemd script.

You can create the file /etc/systemd/system/component.service with the following:

[Unit]
Description=component
Wants=network-online.target
After=network.target network-online.target

[Service]
ExecStart=/path/to/start.sh

[Install]
WantedBy=multi-user.target

You mentioned your component requires networking, which is what the Wants= and After= lines guarantee.

Once you have the file in place you need to reload systemd with the command sudo systemctl daemon-reload

To get it to automatically start on boot, run sudo systemctl enable component.service

There are a number of commands you can run against the service with the format sudo systemctl <command> component.service:

  • disable - stop the service from automatically starting on boot
  • start - start the service now
  • stop - stop the service now
  • restart - restart the service
  • status - see the current status of the service

You can also get a log of the programs output with the command journalctl -u component.service

Be mindful that if your startup script exits (based on your question it doesn't seem to, but perhaps it does), you may need to use a "oneshot" service setup rather than the one listed above. Take a look at this existing SO post for the difference: https://stackoverflow.com/questions/39032100/what-is-the-difference-between-systemd-service-type-oneshot-and-simple

Hope this helps.

1

So after reading about the difference between TTY and PTY I realised I could just stop starting the terminal at startup and an emulated terminal in the background will still call .bashrc. So my solution was to remove @lxterminal from ~/.config/lxsession/LXDE-pi/autostart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.