<SOLVED> How to make a script that runs on user log on?

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
haddy
Level 1
Level 1
Posts: 24
Joined: Fri Jan 07, 2022 8:02 am

<SOLVED> How to make a script that runs on user log on?

Post by haddy »

Hi!
Total linux noob here.

I have been trying very very hard to get my wacom tablet to work the way I want on Mint MATE.

I finally figured out how to use xsetwacom command but unfortunately it is not persistent. When my computer restarts it doesn't remember any of my settings.
Basically there is some xorg.conf file somewhere but it apparently has limits on what can actually be put there (like keybinds to something other than mouse inputs), so I don't want to use that option.

The other option is to make a "script" that automatically applies all of these terminal commands on startup, or even better on user login.
Here are a few of the commands I want it to run when I login:

Code: Select all

$ xsetwacom set "Wacom Bamboo 16FG 4x5 Pen stylus" Area 0 0 14720 8276
$ xsetwacom --set "Wacom Bamboo 16FG 4x5 Pen stylus" Rotate HALF
there are more but I won't bore you with the details.

Basically is it possible to write a script that will automatically just basically copy paste these things into the terminal without me noticing or having to do anything when I logon? And if it is possible can someone walk me through how exactly to do it?

Thanks.

edit:
Solution:
I made a new directory under my username dir and called it "bin"

Code: Select all

/home/haddy/bin
Inside of this directory I made a new file and called it "wacomsetup.sh"
right click to properties on the file and select "permissions" and check "allow executing file as program". Here also I set the permissions to myself as "read and write" and others as "read only".

Then open the file with a text editor and start with

Code: Select all

sleep 10;
because I am almost certain you will have strange problems if you start trying to run things before everything on your computer is fully loaded. This delays the commands from running until 10 seconds(?) go by when you start your computer.

Anyway then I added the commands that I wanted to run after:

Code: Select all

sleep 10;
xsetwacom --set "Wacom Bamboo 16FG 4x5 Pen stylus" Rotate HALF;
xsetwacom --set "Wacom Bamboo 16FG 4x5 Finger touch" Rotate HALF;
xsetwacom --set "Wacom Bamboo 16FG 4x5 Pen eraser" Rotate HALF;
xsetwacom set "Wacom Bamboo 16FG 4x5 Pen stylus" Area 0 0 14720 8276;
xsetwacom set "Wacom Bamboo 16FG 4x5 Pen eraser" Area 0 0 14720 8276;
and so on.

On MATE search for a program called "startup applications"
press "add"
give it a name. (I called mine "wacomsetup")
under command there is a little button called "browse", I click it and find my file in the filesystem and then click on it. It automatically fills in the correct location

Code: Select all

/home/haddy/bin/wacomsetup.sh
I put a nice comment in "configures WACOM on startup" for even more clarity in the "Comment" section.
Finally I clicked "Add" and it shows up in the list of startup apps.
Now when I restart my computer it automatically applies all my tablet settings for me without me having to do anything. Nice!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
dave0808
Level 5
Level 5
Posts: 986
Joined: Sat May 16, 2015 1:02 pm

Re: How to make a script that runs on user log on?

Post by dave0808 »

Probably the simplest option is to use the 'autostart' mechanism.

First use the text editor program and add all the commands that you want to run.

Save the file in your home directory, with a suitable filename. For example wacomsetup or anything that is not likely to get confused with a system command. By convention, you would also give the filename an extension of .sh but this is entirely optional.

Then either:-
a) use the file manager, right click on the file you just created, select "Properties...", select the "Permissions" tab at the top, then make sure that there is a tick in the checkbox for "Allow this file to run as a program". Make sure that the "Access" for "Others" is set to "Read only" or "None" - that is, no-one else can write to this file.
b) alternatively open a terminal and run the command chmod 700 wacomsetup (or whatever filename you used).

Now within a terminal, check that all of your commands in that file are correct by running it directly using ./wacomsetup (or whatever filename you used). Make sure that there are no errors. If so, edit the file and correct the command(s).

Finally, this is for XFCE, but the other desktops should be similar... go to the start menu and find the "Session and Startup" program. Click on the "Application Autostart" tab. Click on the + button towards the bottom. Enter a name that's useful to you. Enter a Description if you want to. Click on the button to the right of the "Command" and select the file you just created. Select when you would like it to run, probably "on login". Press OK. That should be it.

Longer term, you'd likely want the file in a sub-directory rather than littering the home directory. Also, if you do a fresh installation, then you will need to do the above again. However, the changes will persist for any upgrade as the file is part of your data, and the "autostart" is stored locally in your config.
dave0808
Level 5
Level 5
Posts: 986
Joined: Sat May 16, 2015 1:02 pm

Re: How to make a script that runs on user log on?

Post by dave0808 »

... and for Cinnamon, the program is called "Startup Applications", within "Preferences". Click + and choose "Custom Command". Name and Command are as per XFCE. Comment is for any optional Description. Enter a delay of 0 in order to have the command invoked when you login.
rickNS
Level 9
Level 9
Posts: 2956
Joined: Tue Jan 25, 2011 11:59 pm

Re: How to make a script that runs on user log on?

Post by rickNS »

dave0808 wrote: Thu Jan 13, 2022 12:19 pm ... and for Cinnamon, the program is called "Startup Applications", within "Preferences". Click +
OP states using MATE, which also has the "Startup Applications" program, but Click "Add", then fill in the blanks with your custom script name, click add again
ksnip220113-1533.png
Only other thing is "probably" the best place to put your homegrown scripts is in a /home/$USER/bin directory. That way you won't need the path to the script, just the script name will run it.
After you make the directory mkdir ~/bin log out, and back in. Now /home/$USER/bin will be in your $PATH. You can check by running $PATH command.
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
ajgreeny
Level 7
Level 7
Posts: 1652
Joined: Mon Nov 19, 2007 3:27 pm

Re: How to make a script that runs on user log on?

Post by ajgreeny »

I suspect you may need to wait for the x-session to be up and fully running before you can successfully get that script to do what you want so I suggest the first line of the script following the #!/bin/sh needs to delay the commands being run for maybe 10 seconds.
So try the following.

Code: Select all

#!/bin/sh
sleep 10;
xsetwacom set "Wacom Bamboo 16FG 4x5 Pen stylus" Area 0 0 14720 8276;
xsetwacom --set "Wacom Bamboo 16FG 4x5 Pen stylus" Rotate HALF;
<other commands>
User avatar
Koentje
Level 7
Level 7
Posts: 1571
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How to make a script that runs on user log on?

Post by Koentje »

ajgreeny wrote: Thu Jan 13, 2022 5:42 pm

Code: Select all

#!/bin/sh
sleep 10;
xsetwacom set "Wacom Bamboo 16FG 4x5 Pen stylus" Area 0 0 14720 8276;
xsetwacom --set "Wacom Bamboo 16FG 4x5 Pen stylus" Rotate HALF;
<other commands>
Just a question.. why the ; at the end of the lines??
I thought that was only necessary if you have a one-liner.. in a script i never use them at the end.
Just curious about that.
iliketrains
Level 4
Level 4
Posts: 241
Joined: Wed Aug 26, 2020 6:32 pm

Re: How to make a script that runs on user log on?

Post by iliketrains »

If the script runs on start-up what happens if you hot-plug ? Maybe you have it plugged in always. My wacom settings script runs from a desktop launcher, the launcher is clicked-on when needed. Also could be useful method if settings are different for different applications
This is on xfce desktop so I hesitate to guess at the exact details for MATE.
iliketrains
Level 4
Level 4
Posts: 241
Joined: Wed Aug 26, 2020 6:32 pm

Re: How to make a script that runs on user log on?

Post by iliketrains »

Mark this post down to a bout of insomnia :)
Adding a bit to what's already been said, there's a nice tutorial on bash scripts here:
https://stackoverflow.com/questions/217 ... n-terminal
About that PATH thing, you can do this:

Code: Select all

echo $PATH
Then create a bin folder in your home directory. Next time you log in, running, 'echo $PATH' will show something new. Run the command 'cat .profile' if your curious why that happened. Mint should have that folder by default imo.
The bash script can be like this:

Code: Select all

#!/bin/bash
xsetwacom (whatever)
For my use I'm sticking to the desktop launcher idea. No need to consider 'sleep' in the script due to uhmm....the-order-of-things at start-up. Ignore what I said in the above post about hot-plugging, if the cat (yeah we're that kind of household) pulls the cord the script can take care of putting things in order.

* Edited 'path thing' to 'PATH thing' to avoid mixing concepts.
haddy
Level 1
Level 1
Posts: 24
Joined: Fri Jan 07, 2022 8:02 am

Re: How to make a script that runs on user log on?

Post by haddy »

ajgreeny wrote: Thu Jan 13, 2022 5:42 pm I suspect you may need to wait for the x-session to be up and fully running before you can successfully get that script to do what you want so I suggest the first line of the script following the #!/bin/sh needs to delay the commands being run for maybe 10 seconds.
So try the following.

Code: Select all

#!/bin/sh
sleep 10;
xsetwacom set "Wacom Bamboo 16FG 4x5 Pen stylus" Area 0 0 14720 8276;
xsetwacom --set "Wacom Bamboo 16FG 4x5 Pen stylus" Rotate HALF;
<other commands>
Correct. Without delay it completely breaks my tablet and mouse touchpad. I thought my PC was possessed because the mouse kept shaking like crazy and it kept zooming in and out of the window. It was terrifying.
haddy
Level 1
Level 1
Posts: 24
Joined: Fri Jan 07, 2022 8:02 am

Re: How to make a script that runs on user log on?

Post by haddy »

iliketrains wrote: Sat Jan 15, 2022 3:41 am Mark this post down to a bout of insomnia :)
Adding a bit to what's already been said, there's a nice tutorial on bash scripts here:
https://stackoverflow.com/questions/217 ... n-terminal
About that PATH thing, you can do this:

Code: Select all

echo $PATH
Then create a bin folder in your home directory. Next time you log in, running, 'echo $PATH' will show something new. Run the command 'cat .profile' if your curious why that happened. Mint should have that folder by default imo.
The bash script can be like this:

Code: Select all

#!/bin/bash
xsetwacom (whatever)
For my use I'm sticking to the desktop launcher idea. No need to consider 'sleep' in the script due to uhmm....the-order-of-things at start-up. Ignore what I said in the above post about hot-plugging, if the cat (yeah we're that kind of household) pulls the cord the script can take care of putting things in order.

* Edited 'path thing' to 'PATH thing' to avoid mixing concepts.
I'm too stupid to understand what PATH thing does or means or whatever and frankly I don't care. The autostart program on MATE lets you direct it to a particular file through your file explorer. I set the correct file manually through there.
Under the "command" setting:

Code: Select all

/home/haddy/bin/wacomsetup.sh
and it worked. I still made a bin though and echo $PATH does seem to somehow find it and list the location first on its list:

Code: Select all

haddy@haddy-ThinkPad-11e:~$ echo $PATH
/home/haddy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I guess it's just looking for the keyword "bin" in every directory or something?
haddy
Level 1
Level 1
Posts: 24
Joined: Fri Jan 07, 2022 8:02 am

Re: How to make a script that runs on user log on?

Post by haddy »

Thank you everybody for posting.
I now have a script that runs on startup and makes all my tablet settings go all pretty like. I am going to mark this topic as solved.

edit:
in case anyone is curious this was all for the grand goal of getting my WACOM tablet configured correctly.
here is the topic where I suffer through figuring that out:
viewtopic.php?f=49&t=365174&p=2124348#p2124348

I noticed even iliketrains made it in here. Hi @iliketrains!!!
iliketrains
Level 4
Level 4
Posts: 241
Joined: Wed Aug 26, 2020 6:32 pm

Re: <SOLVED> How to make a script that runs on user log on?

Post by iliketrains »

I'm too stupid to understand what PATH thing does
No, I posted WAY to much information
does or means or whatever and frankly I don't care
And that's fine, but I'm going clean up my mess anyway if that's okay. Others may read my previous bafflegab.
If there is a directory named 'bin' in the home directory
And a script in that directory is executable (run command chmod +x script_name)
Then no more /path/to/script_name
or ./script_name
or caring where script_name is
or caring which directory we a currently in
just use 'script_name' like any other command

bin in home directory has a special purpose and makes things easier.
Locked

Return to “Beginner Questions”