Is there a way to turn off and on samba service using crontab

For some reason every few days, 2 to 3, my samba share stops working.
Only way it works again if I turn off and turn back on the share by going to Storage -->Samba—>samba services—>off—>on
I looked in to Samba logs but get nothing useful there.
This is for my home setup and not a mission critical thing.
I was thinking if I could run a scheduler every few days to turn off the service and turn back on.
I did some googling but no help.

@meazz1
you could try to run the command below to ensure that the settings are correct for the most part, and not inadvertently cause a stopping of your samba service:
testparm /etc/samba/smb.conf

further, sometimes you will see some information using:
journalctl -xe
that could possibly give you an idea why it stops.

I believe, you can use crontab to schedule a restart of the samba service. It would involve using this command line:
systemctl restart smb
or, of course --> systemctl stop smb and then systemctl start smb

So, daily restart at 3 in the morning, I believe, would look like this in your crontab -e

0 3 * * * systemctl restart smb, and of course, if you want to send the output somewhere, you can do that, too (like email, files, etc.) :

MAILTO="fritze@gmail.com"
0 3 * * * systemctl restart smb 

or

0 3 * * * systemctl restart smb > /home/<username>/<randomdirectory>/cron_output.txt

but I haven’t used crontab very much, so you probably want to double-check that this would be the right set up.

3 Likes

Thanks is worked out perfectly.
I have 2 cron job running and it’s sweet.
0 3 * * * systemctl stop smb
0 4 * * * systemctl start smb

3 Likes