[Solved] Launch command once at login per user.

SpongeB0B

Member
Joined
Feb 11, 2022
Messages
30
Reaction score
7
Credits
270
Hi everyone,

When I create an user ( useradd ) I would like to create a command to run only once for the user when he login for the first time.

First I was thinking about ~/.profile but I don't like it because the command will stay forever in the file..

and if I use a solution like https://www.attosol.com/login-script-that-runs-only-once-per-user/ it create garbage files :/

Do you have an idea of a solution that will deleted once it had run ?

Thx
 


Whilst in the users home directory, here is a script that will run a command, and then remove itself. In the code below, the command "touch newfile" will create a file called "newfile" if the file ".profile" exists. You could write this code into a file called "disappear", give it execute permissions, and then run it in your home directory, and it will remove itself upon execution. You can make the command whatever you like.

Code:
#!/bin/bash
if [ -e $HOME/.profile ] 
then 
    touch $HOME/newfile
    rm $0
fi

To accomplish the rest of your wishes, to run a command once when the new user logs in for the first time, you will need to do few other things. There are going to be a number of possible methods. Here is one route.

Add the "disappear" script (with modifications like those suggested below) to /etc/skel with execute permissions. You need to be root to do that. The files and dotfiles in /etc/skel are placed in the new user's directory when the new user is created by the adduser command. You will see that .profile file among others in /etc/skel. All these files will be placed in the new users home directory with the user's permissions.

Place the command "./disappear" at the end of the /etc/skel/.profile file, say, for example, on line 29.

The "disappear" script above needs to be modified so that it not only runs the command you want to run (in the example above it's "touch newfile"), but also removes that line 29 in .profile with the "./disappear" command and restores the .profile to its original code so that no trace is left of that "./disappear" command. To do that you could add to the code above, after the touch command a couple of lines like:
Code:
sed '29d' $HOME/.profile > /tmp/profile
cat /tmp/profile > $HOME/.profile

This code will remove from .profile the "./disappear" command if it's on line 29, and return it to it's original code, and remove itself. No traces will be visible to the user in their home directory. There's perhaps some inelegance to this, but it worked. I have hard coded the line 29 which in normal scripting I wouldn't do but would need more code to find the appropriate line in .profile to delete. I was more concerned with proof of concept for the moment.
 
Last edited by a moderator:
Thank you @NorthWest ! I will try this now (and will keep you posted of the outcome)


I'm a newbie in bash scripting and I don't understand the mechanism of the line 5 ( rm $0 )

Bash:
#!/bin/bash
if [ -e $HOME/.profile ]
then
    touch $HOME/newfile
    rm $0
fi

Does someone have the explaination of it ? $0

Never mind I found $0 = name of the command :)
 
Last edited:
@NorthWest I confirm it worked !
content.gif

Thanks a lot
 
Thanks SpongeBob for getting back on the issue. Much appreciated.
 

Members online


Top