r/linux4noobs 5d ago

shells and scripting How to Write A Start-Up Script in Linux Mint Cinnamon?

Hi everyone, I would like to write a simple start-up script in Linux Mint Cinnamon 22.3 64-bit. The reason I would like to write the start-up script is to automatically change settings for my sound card that always selects the wrong output, which then requires me to manually change through the terminal. My sound card is a Creative Sound Blaster AE-7. Here is what I would like the start-up script to do:

  1. Open terminal

  2. Type " alsamixer "

  3. Press enter

  4. Press F6

  5. Arrow key down one space to select “ default:0 HDA Creative ”

  6. Press enter

  7. Right arrow key 26 times until it lands on “ <Output S> “ which displays Headphones

  8. Arrow key down once to display “ Speakers “

  9. Press the escape key

  10. exit terminal

Would anyone be willing to offer advice as to how I could go about doing this?

Upvotes

17 comments sorted by

u/doc_willis 5d ago edited 5d ago

it seems odd you are using 'alsamixer' in this day when pulseaudo and pipewire exist. But I dont use Mint, so perhaps its some mint weirdness, or you may be you are going about things the wrong way.

alsamixer has a feature to 'save' the current settings, I thought they got restored automatically when a user logged in. But again, that may be pulseaudio or some other audio mixer doing that.

https://askubuntu.com/questions/50067/how-to-save-alsamixer-settings

basically set the mixer how you like, then run

sudo alsactl store

This should save alsamixer configurations to /etc/asound.state which gets loaded every startup.

https://www.geeksforgeeks.org/linux-unix/alsactl-command-in-linux-with-examples/

You really should not need to make a startup script to restore audio settings.

But that askubuntu url has info on making a .desktop file that 'restores' the alsa settings, that you could put in your users autostart directory if desired.

You could even setup various sound card 'states' in different config files and have a icon/launcher for each one.

u/Loveschocolate1978 5d ago

I tried that a few times and it just seems to refuse to save the new settings. It's very odd. It's almost as if it reverts back to default settings every time I reboot the system, no matter how many times I save the new configuration.

u/doc_willis 5d ago edited 5d ago

examine the file and permissions, save the config file somewhere else, then manually copy it over perhaps.

Also as mentioned, you can make a .desktop file that launches/loads up any saved config file you make.

you MIGHT need to run the alsamixer tool as root, then run the command to save the file.

look at the config file, then do a change/save, see if the file changes. It may be its changing but not getting reloaded, or it may be its not getting changed at all.

personally I would use pavcontrol, I dont see why you would have to use alsamixer these days.

sudo apt install pavucontrol

https://www.siberoloji.com/how-to-set-up-audio-mixing-with-cinnamon-desktop-on-linux-mint/

u/Loveschocolate1978 4d ago

Do you know where I could go about finding the config files for alsamixer? I didn't know there were other options beyond alsamixer that were prolific. I typically try to stick to default apps.

u/doc_willis 4d ago

This should save alsamixer configurations to /etc/asound.state

so it should be in the above file.

I am going to say that while alsamixer may be installed by default, I bet it's not considered the "default" audio mixture settings app on almost any distribution these days 

the pulse audio and pipewire mixers should be used instead.

u/BazzaJH 5d ago

You'd be better off finding the relevant amixer command and just having that run on startup. You will always have a better time making a script do script things, rather than having it emulate human things. Plus it's more stable in case anything changes within that alsamixer TUI in future.

u/Loveschocolate1978 4d ago

That sounds like a good idea for all of the reasons you stated. Do you know where I could find a list of those commands?

u/BazzaJH 4d ago

amixer -h or amixer --help should list everything you need. I don't know exactly what the command would be since it will depend on your devices, but you're probably looking for something like

amixer -c 0 sset 'Output Select' 'Speakers'

Might need to change the -c 0 to a different card number if it's listed differently, or you might be able to drop that part entirely if there's only one card, it should grab it by default. And just be sure that the controls are setting the same thing as you were in the interactive menu.

u/Turbulent_Strain361 5d ago

Have you tried creating a .conf file to set your defaults? Usually something like /etc/asound.conf that contains the pcm and ctl defaults you want? This will likely be much easier than writing the startup script imo.

If you’re set on a startup script you can try making a shell script (.sh) first like should be “#!/bin/sh” the following lines can be the terminal commands you use. If you need to filter you may be able to use grep to filter results, save as a variable, then apply the variable for later commands. To make any shell script run at startup you should be able to add it to /etc/rc.local or create a systemd service.

u/Loveschocolate1978 5d ago

I do believe I attempted this, but I failed on both accounts. If I remember correctly, I would hear the card's relay click over, then back to where I wanted to be, then immediately click back. It's like the setting wouldn't stick. Then I started to have stability issues with my OS so I reinstalled it.

u/Turbulent_Strain361 5d ago

Oh wow. I’m not sure why an audio configuration would cause issues with your OS stability, but I do hope you get it figured out.

Have you tried any alternatives to alsamixer like pulseaudio?

u/Loveschocolate1978 4d ago

I was surprised too! I think maybe I touched something else unintentionally. I had only been using linux for a few weeks at that point, so it doesn't surprise me. I haven't tried any alternatives yet because I typically like to stick with default apps as my previous experience with instability shook me a bit. Maybe it's time to try again?

u/Turbulent_Strain361 4d ago

Maybe it is. I have used pulseaudio and pipewire with easy effects across multiple distros and I’ve heard good things about pavucontrol. The nice thing about Linux is you’re free to use whatever you want really, you have options. Please make sure to let us know when you find the solution that works for you.

u/LudasGhost 5d ago

I would google ‘how to make a startup script in linux’.

u/Loveschocolate1978 5d ago

Tried that. It didn't work. I bricked my distro.

u/LudasGhost 5d ago

I hope you didn’t believe the first result given by the AI. Just yesterday it told me the diameter of Neptune’s orbit is 140 light hours.

u/Loveschocolate1978 5d ago

No, I was pulling from multiple resources and cross-referencing. Still no dice.