Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to run my application when a user logs in. So I've added a script in ~/.bash_profile as follows:

# for Buluttdepo
/usr/bin/myapp > /dev/null 2>&1 &
export MYAPP_HOME=/opt/myapp

It is run successfully, but should I add the script in .bash_profile?

Otherwise I can add in /etc/profile or add a script file as /etc/profile.d/myapp_run.sh . If I do it this way, myapp is run by root. But I don't want to run it by root. I should run by any user.

Therefore, I'm building an RPM package. I want to add the script when the RPM package installer is run. Because the RPM package is installed by root, it doesn't know which .bash_profile to add to, so it can add only to /etc/profile or /etc/profile.d/.

  • Which file should I add it to?
    • If the file is ~/bash_profile, how to add the script in bash_profile for any user by root via rpm?
    • If the file is /etc/profile , how can any user run myapp?
    • Can you suggest a simpler way to do it?
share|improve this question
Why is it mandatory that the application run on login? Must it also run on console login? – Ignacio Vazquez-Abrams May 30 '13 at 11:03
/etc/profile isn't for root only, all users (using a shell which supports /etc/profile) will source it when the shell is a login shell. – Patrick May 30 '13 at 12:16
If I add /usr/bin/myapp > /dev/null 2>&1 & in profile, it is run by root. I want to run for any user. – Gyhot May 30 '13 at 12:33
It sounds that what you want is for myapp to run for each user on each user's login. Is this correct? – mattdm May 30 '13 at 12:44
yes, if a user has login, myapp should is run. – Gyhot May 30 '13 at 13:45

It sounds like cron would solve what you're trying to do.

The general format of a crontab looks something like this, with each column representing the minute, hour, day of the month, month and day of the week, respectively.

30 * * * * /home/user/updates.sh

Cron also supports a few predefined entries such as @weekly, @daily and amongst these is @reboot.

You can either edit the crontab while logged into the user you wish to run the script from using crontab -e or as root crontab -u username -e to edit another user's crontab.

Once you have your editor open, add:

@reboot /usr/bin/myapp > /dev/null 2>&1

The next time you reboot the machine, /usr/bin/myapp will be run by the user whose crontab you put it into.

share|improve this answer

A package installation script touching files in home directories is a definite no-no. You are highly likely to disrupt the user's configuration, and it may or may not work (what if the user has zsh or tcsh as his login shell?), and it only applies for users whose home directory is around at the time the package is installed.

It is extremely strange to run an application whenever a user logs in. It is up to each user to choose whether he wants to run your application.

If you need to execute code when a user logs in, modifying /etc/profile is your best bet. If your distribution has a directory /etc/profile.d, drop a file there instead of modifying /etc/profile. If you must modify /etc/profile, be very careful not to disrupt the system administrator's customizations. Typically all you'd do in profile is to set an environment variable.

You claim that “If I do it this way, myapp is run by root”. That's just false: /etc/profile is executed by the user who's logging in.

To reiterate, what you should probably do is tell users “if you want to run my application, run the myapp command or select ‘My App’ from the menu or click ”, nothing more. Be sure to provide a menu entry for GUI users. To avoid fiddling with environment variables, provide a calling script that sets any necessary environment variable.

share|improve this answer

Your Answer


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.