4

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 Answers2

3

What I would suggest is a 2 step process:

  1. Create a myapp.desktop file in /etc/skel/.config/ directory. Everything under /etc/skel gets 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 .desktop files 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.

  2. 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.desktop
    

    That 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`
0

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.

SdSaati
  • 201
  • 1
  • 9