Create a script that mounts a share, checks a file if it has not been modified in 2 or 1 days, and then unmounts it?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Create a script that mounts a share, checks a file if it has not been modified in 2 or 1 days, and then unmounts it?
Hello
In a monitoring system I have, I had a plugin that remotely checked a SMB share and if the file hasnt been modified in 1 day, it should return a warning. if it is 2 days or more, it should return a critical.
Returning the warning and the critical isnt the issue, its the entire logic of the script itself.
I mean, I dont think it should be too difficult but Im not used to shell scripting. Also, the downfall exists, of having to check if the mount is correct, etc.
Could someone tell me the basics of the shell script?
Mounting the filesystem is done with mount - if it succeeds, the mount exit status will be zero, otherwise it wont.
Unmounting is done with umount (note: not unmount) - you just run that command regardless and use --quiet option to tell it not to complain if it wasn't mounted.
Checking whether a file has been modified is something find can do - you can use the -mtime option with 24 and 48, and then either check the find exit status, or use the -exec action to output the warning, or perhaps instead use -printf format then do the comparison with something else.
I can't tell if the SMB share is linux or other? If it's linux can't you avoid the mount process altogether and just run a find command over ssh? Then react on the results? This also wouldn't require it to be run as root / or with an fstab entry with the user field.
Something generally like this?
Code:
#!/bin/sh
TARGET=$(ssh user@target "find /path/to/folder/ -mtime +1 -type f")
[ -z "$TARGET" ] && (react if not found)
Last edited by jmgibson1981; 05-16-2022 at 09:55 AM.
Location: Montreal, Quebec and Dartmouth, Nova Scotia CANADA
Distribution: Arch, AntiX, ArtiX
Posts: 1,364
Rep:
Quote:
Originally Posted by jmgibson1981
I can't tell if the SMB share is linux or other? If it's linux can't you avoid the mount process altogether and just run a find command over ssh? Then react on the results? This also wouldn't require it to be run as root / or with an fstab entry with the user field.
Something generally like this?
Code:
#!/bin/sh
TARGET=$(ssh user@target "find /path/to/folder/ -mtime +1 -type f")
[ -z "$TARGET" ] && (react if not found)
.... good idea. You can also SSH into a Windows host.
inputvariable warningdays
inputvariable criticaldays
inputvariable smbpath
mount smbpath to final folder /mnt/folder
if mount is unsuccessful
return unknown
if mount is successful
if /mnt/folder/file does not exist
return unknown
if /mnt/folder/file does exist
if /mnt/folder/file has been written to less than warningdays
return ok
if /mnt/folder/file has been written to more than warningdays and less than criticaldays
return warning
if /mnt/folder/file has been written to more than criticaldays
return critical
if previousoperations are done
unmount
if unmount is unsuccessful
return unknown
finish
Read, understand and apply what you were told in post #2. (You may also find Bash Conditional Expressions useful.)
If there's any part of what I've written which isn't sufficiently clear, say so and someone can explain further.
If you attempt to write a Bash script and post that here (after running it through Shellcheck) you'll get advice on any issues, but your post reads a bit like you want someone to just do it for you (if that is what you want, hire a consultant), and LQ is about helping people to help themselves.
your post reads a bit like you want someone to just do it for you
This script at most Id say takes up 20 lines
Reading thru all the documentation would at least take me 30 minutes.
Someone here can problably write up the script in 30 minutes.
Lets say I start writing the script: At least 2 hours since I dont know much (if at all) about shell scripting.
So, in my mind, I see as logical to bill the end client 30 minutes for a POS script vs 2 hours and telling him they have to pay for me (or others) reading the documentation.
You have a close friend that is a mechanic. Are you gonna ask him a question that takes 5-10 minutes to REPLY or are you gonna call up the garage, setup up a appointment, bring your car in, have the mechanic look at it for 1 hour and then tell you the same thing your close friend would? Its counterproductive and time wasting
You should bill the client the time required. If you can't do it yet offered / claimed you could then you screwed up and that is on you. If they don't want to pay it then they can do it. Simple concept. Don't work for free (unless you lied to them and said in which case again... on you).
Last edited by jmgibson1981; 05-17-2022 at 09:39 AM.
Reading thru all the documentation would at least take me 30 minutes.
Someone here can problably write up the script in 30 minutes.
Lets say I start writing the script: At least 2 hours since I dont know much (if at all) about shell scripting.
So, in my mind, I see as logical to bill the end client 30 minutes for a POS script vs 2 hours and telling him they have to pay for me (or others) reading the documentation.
You have a close friend that is a mechanic. Are you gonna ask him a question that takes 5-10 minutes to REPLY or are you gonna call up the garage, setup up a appointment, bring your car in, have the mechanic look at it for 1 hour and then tell you the same thing your close friend would? Its counterproductive and time wasting
Thats my POV
I don't care about your POV.
If the mechanic is on the job and being paid I would expect to pay his employer for the time involved.
If I want to know how to do things for myself then the time spent in learning how to do that is time well spent.
Your posted POV is that our time is owed to you, but since we are all volunteers we do not feel the same.
You make an effort, ask about where you are having problems, we can point out improvements. You make no effort we provide no assistance.
Oh, BTW --- I would volunteer to write the 'minor' script for you at $75 per hour, with a 2 hour minimum paid in advance. That would be my 'consulting fee'
Ahh, you are discounting the time involved in prep (reading the documentation so one knows what is needed / expected) as not worth being paid for. What a concept! Giving away your time for free!!
Last edited by computersavvy; 05-17-2022 at 10:59 AM.
I'd hate for the discussion to degrade into arguments about time, convenience, or who should do the task.
As noted by another member, LQ works more where we help you to accomplish your goal, using your own technical efforts. Because volunteers on this site are not customer service but here to share and discuss technical challenges related to Linux.
That said, I've certainly seen examples where a member has chosen to help freely and they post a solution as well as help you to refine it to work. No guarantees this will happen.
Back to my first sentence about this thread not degrading, let's all please not argue about who would do it, how one would bill for it, and etc.
If the OP only wishes an option for someone to do the effort for them, then there's nothing to add unless a member decides to do so freely, or contacts the OP offline to discuss a possible paid opportunity.
Meanwhile riahc3,
You should not bump this thread to persist with checking to see if someone new sees it and considers providing a solution. And if your interest is to have someone do effort for you, for hire, you can ask people to PM you, but not post a contact email or discuss terms and details within a technical thread. This is not a forum which supports advertising work for hire.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.