Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running Arch-Linux ARM on a Raspberry Pi.

My project goal is to have a barcode scanner attached to the Pi that forwards the scanned messages to a server in a LAN.

To harden the system against power outages etc. and minimize required user interaction, I wish to set it up, so that a certain user is logged in automatically at startup(done and works) and a specific program is to be run at startup(done and works but not as intended).

I've done this so far by adding

exec mono scannerSoftware.exe 127.0.0.1 1234 randomString

to /home/certainUser/.bash_profile so it gets executed only once even when I'm switching back and forth to root (in contrast to bashrc).

The particular problem I am having: When I launch the system it works as intended (login as certainUser, starting scannerSoftware.exe), BUT if I want to switch users and hit Ctrl+C it restarts the login process, logging certainUser in again and running the program again => back to where I came from.
My guess as to why this happens is that the sys detects "login not complete, better start it again"

I could circumvent this by spamming Ctrl+C right around the login, so that I practically interrupted it. Of course this is not good practice, suffice to say it is not practical at all.

My question:
How can I set it up, so the program gets executed AFTER login is complete, so that Ctrl+C'ing out brings me to a normal bash promt as certainUser instead of beginning the endless login-cycle?

EDIT:
I'm not using any graphical interface or such, only commandline (bash on tty), because when done the system won't even have a display.

share|improve this question

4 Answers 4

up vote 4 down vote accepted

The answers depends on your requirements.

You might only want it to start after a particular user logs-in, alternatively you may want it to start when during reboot (a service). ?

start on login

When you start a program with exec it replaces the process that is used to start it.

  • So currently, your login bash is replaced by your scanner and ctrl-c sends interrupt signal, it stops, and you are then logged out.
  • instead of execing you could simply start the program, it runs in foreground (you don't get the prompt back). ctrl-c has the same effect as before, stops the scanner, but you will be back at the login bash prompt.
  • You can start the process in the background, by appending & to the command line, this creates a new process, still displays output on your terminal, but you have a prompt from your login shell as well, so output from the background process could interfere with your foreground shell. You can get over this by redirecting stdout and stderr to a file (even /dev/null if it's not wanted), thus avoiding the interference.

When you logout it's still running in background, it will get a hangup signal (but can catch that), so still processing in the background. If it was attached to your terminal it will become detached (output discarded, unless you had already catered for redirection).

When you login again, then a new scanner will be started, unless you check whether there is already one running, and it's easy to do that.

start during reboot

The advantage with this method is you do not need to login to get the process going, the disadvantage is that it is running all the time, you may not want that.

There are 3 main system start-up mechanisms, init, upstart and systemd, each being different. Traditionally /etc/rc.local would be the place to add machine specific system processes.

As WilQu mentions, Arch uses systemd, and he has provided a recipe for this, so I won't repeat.

advice

Write a script to start your process, even if it's only one line, the chances are that you will want to do more than just execute it, so you might as well write the short script now. (check if it's running, store the pid, use a different random string based on date ? who knows ...).

This allows you to develop and test the script, running it by hand and ensuring it woks as desired.

Then you can add you script, where you want (by hand, login profile, system startup).

share|improve this answer
    
Though WilQu and fauve gave insightful and good answers too, this one did the job for me: I omitted the exec and now I get to the normal bash-promt when the program runs into issues or gets ended with ctl+C. Thanks! –  Mark Feb 27 at 13:31
    
tks for the feedback, good luck with your project. –  X Tian Feb 27 at 13:45

If you just want to launch a program at startup, a login script is the wrong place to do it.

Instead, write a systemd unit file (since Arch seems to use systemd).

Create /etc/systemd/system/scanner.service (or whatever.service) :

[Unit]
Description=(description of your program)

[Service]
ExecStart=/usr/bin/mono /path/to/scannerSoftware.exe 127.0.0.1 1234 randomString

[Install]
WantedBy=multi-user.target

And run systemctl daemon-reload.

Run systemctl enable scanner.service to make it run at startup.

You can start and stop it manually with systemctl start scanner.service and systemctl stop scanner.service and see its status with systemctl status scanner.service.

See Arch Linux help about systemd

share|improve this answer
    
The thing is: I need to capture the input from the barcode scanner, which works like any usb-keyboard. It reads the code and terminates with a newline, equal to me typing barcodeContent and hitting enter. therefore I need the app/service to caputre std and not run in the background. –  Mark Feb 27 at 12:45
    
I don't understand then. Why can't the user just launch the program when he needs it? –  WilQu Feb 27 at 13:26
    
I think my definition of user is a bit different from the standard term here. The device in question together with some clones will be deployed in an industrial environment. The people who will use it have no understanding of the inner workings etc. all they want is that their scan data goes to the right magic place, no questions asked. Therefore I want the device to steer itself to the working point of the app once it gets booted, so people will switch power on, wait a minute and scan and the desired things will happen. There won't be any screen attached to it or anything. –  Mark Feb 27 at 13:37

It depend from your DE but in the most cases, you have to put your command in your ~/.xinitrc file.

.bash_profile is for bash (as he named). If you don’t execute bash, the content of .bash_profile will not be executed.

share|improve this answer
    
Actually I'm working without any graphical interface. Once the solution is in place it will not even have a display attached. Therefore I deemed unneccesary to install the X-Server and have the pi create fancy graphics that nobody will ever see –  Mark Feb 27 at 8:23

if you use bash shell you should use : ~/.bash_profile

if you use Tenex C shell or Same TCSH you should use : ~/.login

if you use C shell you should use : ~/.login

if you use Z shell you should use : $ZDOTDIR/.zlogin

if you use Korn shell you should use : ~/.profile

I answer a better answer in what is the difference between ~/.profile and ~/.bash_profile

share|improve this answer
    
If you carefully read the OP you'll see that I'm already using ~/.bash_profile and it does not work as intended. –  Mark Feb 27 at 8:37
    
oh i reread, Problem doesn't relate to unix world, it's related to mono.please debutg it. or upgrade your mono. –  Mohsen Pahlevanzadeh Feb 27 at 8:39
    
it does work just fine when I execute it manually, plus I replaced the command with exec echo "lol", had a few laughs and then the exact same problem. –  Mark Feb 27 at 8:44
    
At the following article you read how to disable CTRL+C : cyberciti.biz/faq/unix-linux-shell-scripting-disable-controlc –  Mohsen Pahlevanzadeh Feb 27 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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