Page 1 of 1

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

Posted: Sat Oct 07, 2023 2:43 pm
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.

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

Posted: Sat Oct 07, 2023 5:26 pm
by FullScale4Me
Please post your QSI = Quick System Info from menu (Copy for Forum)

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

Posted: Sat Oct 07, 2023 7:07 pm
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?

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

Posted: Sat Oct 07, 2023 7:55 pm
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!

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

Posted: Sat Oct 07, 2023 8:23 pm
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

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

Posted: Sat Oct 07, 2023 8:35 pm
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.

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

Posted: Sat Oct 07, 2023 9:11 pm
by CharlesV
Cool... I get not changing! I should point out that the smb1 protocol is not secure, so make sure your network is :-) .

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

Posted: Sun Oct 08, 2023 8:29 am
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:

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

Posted: Sun Nov 05, 2023 12:19 pm
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.