Start script on logon
Solution 1
Okay, I've found a solution to my initial problem. It's dirty one though.
First of all, using ~/.bash_profile
, ~/.bash_login
, ~/.profile
or even /etc/profile.d/
should be preferred methods instead (here's good reading on the subject). Don't know why it doesn't work for me; perhaps it has something to do with my display manager auto logon workaround, just a guess.
Another solution would be adding script shortcuts to global file that gets executed at startup, for instance /etc/rc.local
.
Since my script requires xserver and user to be logged on, I created initfile which waits after bootup for xserver and user to be logged on and then executes the script:
#! /bin/bash
### BEGIN INIT INFO
# Provides: script_file.sh startup at boot
# Required-Start: $all
# Required-Stop:
# X-Start-Before:
# Default-Start: 2 3 4 5
# Default-Stop:
# Description: runs '/usr/local/bin/script_file.sh --optional --flags' at startup, when xserver has started and YOURUSERNAME is logged on.
### END INIT INFO
case "$1" in
start)
# Wait until X is running and required user logged in:
count=0
until [[ "$(tty)" != "\/dev\/tty1" ]] && [[ "$(who | cut -d' ' -f1 | sort | uniq)" == "YOURUSERNAME" ]]; do
sleep 5
let count=$count+1
if [[ "$count" == "10" ]]; then
echo "Xserver hasn't started or other error occurred. Abort"; exit 1
fi
done
# Run the script as YOURUSERNAME instead of root:
su - YOURUSERNAME -c '/usr/local/bin/script_file.sh --optional --flags'
;;
stop)
:
;;
esac
exit 0
This soulution assumes having automatic log-on set up. As mentioned, it really isn't the way to do it, but works for now.
Solution 2
First create your ~/.bash_login
and make it do something simply (like echo
a phrase.)
echo "echo Look at me go" > ~/.bash_login
Then use bash -l
like @mata said. The -l
flag will run the bash as if it were the login shell (to make sure it reads your settings files.)
Related videos on Youtube
laur
Updated on September 18, 2022Comments
-
laur over 1 year
I understand shell executes commands in either ~/.bash_profile, ~/.bash_login or ~/.profile, whichever is found first. My system has only .profile, yet commands entered there won't get executed on startup/login.
The script I'm talking about is essentially a cron front-end/TO-DO list manager. When user logs in (as in user gets access to his personal files/logs in to his account), the script should execute, checking whether there are any tasks that have failed to remind. If it finds any, zenity prompt will be poped up, asking next input.
-
theabraham over 11 yearsYou can create
~/.bash_login
if it doesn't exist. -
Admin over 11 years~/.bash_login still doesn't work.
-
Admin over 11 yearshow do you test if it doesn't work? have you tried inserting an
echo something...
into~/.profile
and executingbash -l
? -
Nicole Hamilton over 11 yearsBe sure your scripts are executable. You can set this using
chmod +x
. -
laur over 11 yearsTried running my own script from there; also added
> location/filename
, which I thought would be good indicator. -
laur over 11 years@NicoleHamilton afaik those setting files don't need to be executable.
-
Nicole Hamilton over 11 years@LaurAliste You could be right. I wasn't on a Linux machine to check and I wasn't able to find a clear answer by Googling.
-
Nicole Hamilton over 11 yearsAre you starting
bash
using the-l
or--login
option to make it a login shell? If not, it won't look for a.profile
or similar login script. -
vonbrand over 11 yearsCheck bash(1). I think the *profile, *login files are only executed bt the login shell (hat you won't see, as it launches all your envirnoment), while *rc and such are sourced each time a new shell is launched.
-
-
laur over 11 yearsWorks with
bash -l
but not after reboot/logon. -
theabraham over 11 yearstry putting it in the
~/.bash_rc
file, and see if that works. -
laur over 11 yearsIf you mean
~/.bashrc
, then already tried that. -
theabraham over 11 yearsHmm, are these files in the home directory of the user you're trying to login as?
-
laur over 11 yearsYes, and there's only one user besides root.
-
theabraham over 11 yearsAre you sure you're using bash? What does
echo $SHELL
tell you? -
laur over 11 yearsI'm sure it's bash. Where does desktop environment (XFCE in my case) put the commands inserted via startup settings?
-
theabraham over 11 years
~/.bashrc
and related files will only run when you open a shell, not when you start your computer. If you want to run something at startup, you can create a cron job that looks like this@reboot /path/to/script
. I use a mac mostly, so I'm not sure how XFCE work :/ -
laur over 11 yearsI've already tried
@reboot
in cron; that doesn't seem to be working either. (just to be clear, my cronjobs are installed in /etc/cron.d/somefile) -
tink over 11 yearsChances are that the shell(s) you're invoking under XFCE aren't classed as login-shells. Try putting what you need to run on login in something xfce specific. forum.xfce.org/viewtopic.php?id=5550
-
laur over 11 yearsThat's the point - the script I'm writing needs to install itself on any system; it's essential it's run at every logon.
-
Zoredache over 11 years@LaurAliste, please precisely define login. What commands exactly need to be ran? Is this for mounting filesystems or something? If you need something to be used be ran for every 'login' irrespective, of what shell or program is actually performing the login, then you need to look at doing something with PAM.
-
Zoredache over 11 yearsBTW, please update your question with additional details, and requirements.