How do I run a system command at startup and shutdown?  [Solved]

Help for Current Versions of MX
When asking for help, use Quick System Info from MX Tools. It will be properly formatted using the following steps.
1. Click on Quick System Info in MX Tools
2. Right click in your post and paste.
Message
Author
davemx
Posts: 320
Joined: Sun Aug 12, 2018 2:31 pm

How do I run a system command at startup and shutdown?  [Solved]

#1 Post by davemx »

I'm in the UK and I've recently changed internet provider. I'm now with PlusNet. They have provided a router which has a USB socket. The USB socket can be used for shared storage across the network. Sadly, the router uses Samba version 1, and there don't seem to be any plans to update its firmware to provide a later version. For information, the router lives at 192.168.1.254 (most are at 192.168.0/1.0/1 but PlusNet and BT routers use 1.254).

I have amended /etc/samba/smb.conf to include in the [Global] section:

Code: Select all

   hosts allow = 192.168.1.254
   min protocol = NT1
Despite this, the "Configure Samba" option in MX Tools doesn't detect anything.

I have created a folder /mnt/router and have found that the following line of code mounts a USB stick in the socket:

Code: Select all

mount -t cifs //192.168.1.254/usb1/ /mnt/router -o vers=1.0,noperm,guest
However, it can't be unmounted in the usual way, if you try it just hangs for a while before returning to the command line without unmounting. You have to add the -i switch:

Code: Select all

umount -i /mnt/router
If you don't do this, it will cause delays on shut down.

I know how to run commands on Startup and Shutdown as a user when logging in, my question is, how do you do it when starting the system and closing it down?

EDIT: In MX Linux, at least when using init.d, there's no noticeable delay on shut down.
Last edited by davemx on Sat Oct 07, 2023 7:59 pm, edited 2 times in total.
Desktop: Mini-Box M350 with Asus H110i-plus motherboard, Pentium G4600 processor, 2TB SSD and 16Gb RAM DDR4-2133
Printer/Scanner: Brother MFC-J5335W
Laptop: Lenovo V15 ADA
Media Centre: Lenovo Q190

User avatar
FullScale4Me
Posts: 1007
Joined: Fri Jan 08, 2021 11:30 pm

Re: How do I run a system command at startup and shutdown?

#2 Post by FullScale4Me »

Please post your QSI = Quick System Info from menu (Copy for Forum)
Michael O'Toole
MX Linux facebook group moderator
Dell OptiPlex 7050 i7-7700, MX Linux 23 Xfce & Win 11 Pro
HP Pavilion P2-1394 i3-2120T, MX Linux 23 Xfce & Win 10 Home
Dell Inspiron N7010 Intel Core i5 M 460, MX Linux 23 Xfce & KDE, Win 10

User avatar
MadMax
Posts: 483
Joined: Wed Aug 04, 2021 3:25 pm

Re: How do I run a system command at startup and shutdown?

#3 Post by MadMax »

For startup you can run a cronjob for the root user with the time setting "@reboot". I don't know a way to do the same for shutdown. Maybe create a small script file that contains the unmount command followed by the actual shutdown (and maybe a short sleep after the unmount for good measure) and use this to power off the system e.g. from the desktop?
If it ain't broke, don't fix it.
Main: MX 23 | Second: Mint 22 | HTPC: Linux Lite 7 | VM Machine: Debian 12 | Testrig: Arch/FreeBSD 14 | Work: RHEL 8

davemx
Posts: 320
Joined: Sun Aug 12, 2018 2:31 pm

Re: How do I run a system command at startup and shutdown?

#4 Post by davemx »

MadMax wrote: Sat Oct 07, 2023 7:07 pm For startup you can run a cronjob for the root user with the time setting "@reboot". I don't know a way to do the same for shutdown. Maybe create a small script file that contains the unmount command followed by the actual shutdown (and maybe a short sleep after the unmount for good measure) and use this to power off the system e.g. from the desktop?
Hi Max, you were on the right road, but it didn't work. I'm thinking that maybe I'd have to log in as root for the cronjob to happen. But you got me thinking and I worked it out.

I simply added the line staring mount -t cifs to /etc/rc.local

As for the unmounting part, it turns out that it's not a problem. I alternate on different root partitions between two distros, and in the other distro, it caused a long delay during reboot/shutdown if I didn't do the umount command, But for some reason in MX it did not.

Of course, using rc.local will mean that if I log in using systemd, it probably won't work. But that's as likely as hell freezing over.

Thanks!
Desktop: Mini-Box M350 with Asus H110i-plus motherboard, Pentium G4600 processor, 2TB SSD and 16Gb RAM DDR4-2133
Printer/Scanner: Brother MFC-J5335W
Laptop: Lenovo V15 ADA
Media Centre: Lenovo Q190

User avatar
CharlesV
Global Moderator
Posts: 7076
Joined: Sun Jul 07, 2019 5:11 pm

Re: How do I run a system command at startup and shutdown?

#5 Post by CharlesV »

There are two things here I see...

First, if you are going to connect via samba, you need the right protocols. the samba config to bring that protocol down to NT1 is

Code: Select all

client min protocol =NT1
There are two parts of samba; server and client. you are after client if your attempting to connect TO a device.

The second thing I see... I connect to my NAS using a *media* mount when I want it ( or on startup ) using a script. AND, when using the media mount, it seems to let go differently. (And USB's "should" be mounted as media and not mnt (According to different docs I have read - AND the system treats them as REMOVABLE media, where real devices are connected.)

SO.. I would try to make your connection in a /media/router mount point and see how it does.

I use a script upon login if I want to do the mount to my NAS, and when I log out or reboot... it just lets go quickly and without issue. Might be worth a try

edited: removed space at NT1
*QSI = Quick System Info from menu (Copy for Forum)
*MXPI = MX Package Installer
*Please check the solved checkbox on the post that solved it.
*Linux -This is the way!

davemx
Posts: 320
Joined: Sun Aug 12, 2018 2:31 pm

Re: How do I run a system command at startup and shutdown?

#6 Post by davemx »

It's working perfectly now, I'm touching nothing!

I prefer /mnt because it's always connected when the computer is on.

I think that if I don't specify client or server in smb.conf it implies both, which is why it works.
Desktop: Mini-Box M350 with Asus H110i-plus motherboard, Pentium G4600 processor, 2TB SSD and 16Gb RAM DDR4-2133
Printer/Scanner: Brother MFC-J5335W
Laptop: Lenovo V15 ADA
Media Centre: Lenovo Q190

User avatar
CharlesV
Global Moderator
Posts: 7076
Joined: Sun Jul 07, 2019 5:11 pm

Re: How do I run a system command at startup and shutdown?

#7 Post by CharlesV »

Cool... I get not changing! I should point out that the smb1 protocol is not secure, so make sure your network is :-) .
*QSI = Quick System Info from menu (Copy for Forum)
*MXPI = MX Package Installer
*Please check the solved checkbox on the post that solved it.
*Linux -This is the way!

User avatar
MadMax
Posts: 483
Joined: Wed Aug 04, 2021 3:25 pm

Re: How do I run a system command at startup and shutdown?

#8 Post by MadMax »

davemx wrote: Sat Oct 07, 2023 7:55 pmHi Max, you were on the right road, but it didn't work. I'm thinking that maybe I'd have to log in as root for the cronjob to happen.
Well, root is kinda always logged in in the background to run system jobs. This can't be the issue. I run this procedure to mount a ramfs on startup, since ramfs can't be mounted via fstab, and it works flawlessly.

But glad you got it working anway. Running it as autostart program via rc.local is a good solution, too :happy:
If it ain't broke, don't fix it.
Main: MX 23 | Second: Mint 22 | HTPC: Linux Lite 7 | VM Machine: Debian 12 | Testrig: Arch/FreeBSD 14 | Work: RHEL 8

davemx
Posts: 320
Joined: Sun Aug 12, 2018 2:31 pm

Re: How do I run a system command at startup and shutdown?

#9 Post by davemx »

I revisited this, and found a neater solution. Added this line to /etc/fstab and removed the rc.local addition:

Code: Select all

#Entry for Router share :
//192.168.1.254/usb1 /mnt/router cifs guest,vers=1.0,iocharset=utf8,file_mode=0777,dir_mode=0777,noperm 0 0
I'll report back again if I have to tweak it a little.
Desktop: Mini-Box M350 with Asus H110i-plus motherboard, Pentium G4600 processor, 2TB SSD and 16Gb RAM DDR4-2133
Printer/Scanner: Brother MFC-J5335W
Laptop: Lenovo V15 ADA
Media Centre: Lenovo Q190

Post Reply

Return to “MX Help”