I know Ubuntu supports autostart of apps every time a user logs in. But what I would like to do is to open a specific app every time a new user logs in for the first time and then the app won't autostart on any login. Is this possible?
2 Answers
What I would suggest is a 2 step process:
Create a
myapp.desktopfile in/etc/skel/.config/directory. Everything under/etc/skelgets copied to new users' folders when they get created. For the format of that file, look into How to add a shell script to launcher as shortcut. Don't worry about the title - the.desktopfiles are essentially same as Windows shortcuts, and their presence in user's~/.config/autostart/directory is what enables particular app to run upon GUI login.Make the wrapper script that will run the app you want but then also remove the specific launcher, say for example
#!/bin/bash gedit rm ~/.config/autostart/myapp.desktopThat way the autostart entry will run only on first login, and on that first login - remove itself. Potentially, you also may want to remove the script itself if necessary. Deleting is also not necessary, you could just rename it via
mv ~/.config/autostart/myapp.desktop ~/.config/autostart/myapp.desktop.bak
Please note, that you didn't specify what particular app or what specific conditions you need for that app to work, so this is only a rough example, and feel free to adapt it to your needs.
On side note, a script may not even be necessary if what you wanna do is simple. For example, you may want to have the line as follows in your .desktop file:
Exec=bash -c 'firefox && rm ~/.config/autostart/myapp.desktop' myapp`
- 105,154
- 20
- 279
- 497
-
+1 have a run once script remove itself was along the lines of my thinking. – WinEunuuchs2Unix Jun 17 '18 at 01:31
-
I get it. But I have a question. Why do I have to write "myapp" as the last word, in the last line `Exec=bash -c 'firefox && rm ~/.config/autostart/myapp.desktop' myapp``. What if I don't have an app called myapp? I want to start firefox at login, Should I change myapp with firefox? – Lucifer Conrad Reeves Jun 17 '18 at 05:24
-
@LuciferConradReeves When
bash,kshor/bin/shshells use the-cflag, the positional parameter specified on the command gets assigned into$0variable, which normally holds the shell or script name. This is not so much a requirement as a good practice in this case. So you can place almost anything there instead. If you are writing a command that does involve processing positional parameters, then you have to place something there, otherwise you'll be confused as to why the shell doesn't process the very first item :) – Sergiy Kolodyazhnyy Jun 17 '18 at 05:32 -
I just want to launch firefox, nothing else. What should I write there? I am still unable to get you. (I am nothing more than a newbie) – Lucifer Conrad Reeves Jun 17 '18 at 05:36
-
@LuciferConradReeves Just write
Exec=bash -c 'firefox && rm ~/.config/autostart/myapp.desktop'Don't worry about the positional parameters. Did you read the linked answer about creating.desktopfiles ? – Sergiy Kolodyazhnyy Jun 17 '18 at 05:47 -
yes it's possible. I have and idea but maybe there are better solutions too, here is my idea hope it help ;)
if you know a little about shell programing language or python or ... you can make a script that whenever you run this script, it get your username and create a text file/a row in database/ a row in a file and before this step, it checked already if this username had been in the database file or not, if not, then it runs your program and if yes, it don't. the python code can be like this (save it as Runit.py) :
import sys
import getpass
import os
def createFile():
f = open("usersFirstLogin.txt", "a+")
f.write(user)
f.close()
os.system("echo Hi") # Run YOUR PROGRAM HERE
return 0
user = getpass.getuser()
fr = open("usersFirstLogin.txt", "a+")
count=0
for x in fr:
if(x == user):
count+=1
continue # DO NOTHING HERE
else:
createFile()
count+=1
if (count == 0):
createFile()
fr.close
but now, how we can set this to run at the start? it's easy with systemd. suppose we make this python script with name Runit.py we need to :
sudo touch /etc/systemd/system/RunMyScript.service
then with
sudo gedit /etc/systemd/system/RunMyScript.service
type these in it :
[Unit]
Description=RunIt
[Service]
WorkingDirectory=/Path/to/the/Runit.py folder
ExecStart=/Path/to/the/Runit.py folder/Runit.py
[Install]
WantedBy=multi-user.target
now your service is ready for start at login :
sudo systemctl enable RunMyScript.service
in this scenario your service always run for each login and your script check whether if for that user already ran or not.
- 201
- 1
- 9
-
Wait, how's this even useful to me? I don't know shell programming or python. For example, I would like to start firefox every time a new user logs in, but implementing this would certainly not start it. Plus I also don't understand more than half of your answer. – Lucifer Conrad Reeves Jun 16 '18 at 23:24
-
I wrote the python code for you, I just run it and it works, you need to change the line
os.system("echo Hi") # Run YOUR PROGRAM HEREso that instead ofecho Hiyou need to put your specific command for example /usr/bin/firefox, sorry if it's not clear, please tell me where you have problem ? – SdSaati Jun 16 '18 at 23:50 -
The problem is that it doesn't work. I don't understand half of the terms – Lucifer Conrad Reeves Jun 17 '18 at 05:20
run-once.shbash script (or whatever it is called) to the user home. What does the script do? Something like display a splash screen such as "Welcome to XYZ Corporation, Don't forget to make a new pot of coffee if you take the last cup"? – WinEunuuchs2Unix Jun 17 '18 at 01:25run-once.sh? How do I use it? – Lucifer Conrad Reeves Jun 17 '18 at 05:16