LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-16-2022, 05:53 AM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Rep: Reputation: 1
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?

Thank you.
 
Old 05-16-2022, 08:20 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,605

Rep: Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548

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.

For checking exit status, you can usually just do if command, but sometimes reading the value of $?, and sometimes chaining with "command1 && command2 && command3". (See also BashGuide/TestsAndConditionals.)

Whatever you do, using Shellcheck is highly recommended.

 
Old 05-16-2022, 08:28 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Sounds like all you need to do is run that plug-in and then test the result.

Everything you can type into the command line, you can put into a shell script.

For when you need to make decisions or iterate, she'll scripts support if-elseif-else and loops.

So suggest you write down your steps in order and then assess if you need more than <step>-<step>-<step> and instead need a loop or if-test.

I think you'll only need either an if-test or a case statement.

We can help you with the logic to write your tests and/or loops.

Finally this seems like it will run periodically, so a cron job is an excellent way to run a script on a periodic basis.
 
Old 05-16-2022, 09:54 AM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.
 
1 members found this post helpful.
Old 05-16-2022, 02:18 PM   #5
Rickkkk
Senior Member
 
Registered: Dec 2014
Location: Montreal, Quebec and Dartmouth, Nova Scotia CANADA
Distribution: Arch, AntiX, ArtiX
Posts: 1,364

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Quote:
Originally Posted by jmgibson1981 View Post
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.

Rick
 
Old 05-17-2022, 08:31 AM   #6
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
My logic code would be this:

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

How would I write that in a shell script?
 
Old 05-17-2022, 09:01 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,605

Rep: Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548
Quote:
Originally Posted by riahc3 View Post
How would I write that in a shell script?
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.

 
Old 05-17-2022, 09:15 AM   #8
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by boughtonp View Post
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

Thats my POV
 
Old 05-17-2022, 09:37 AM   #9
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.
 
Old 05-17-2022, 10:56 AM   #10
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by riahc3 View Post
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

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.
 
Old 05-18-2022, 02:23 AM   #11
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by computersavvy View Post
I don't care about your POV.
Well, you did reply to the post. Hypocrisy at its best.
 
Old 05-18-2022, 02:25 AM   #12
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jmgibson1981 View Post
You should bill the client the time required.
Why?

Over half of the things any IT person does is Google where more than 25% of the time, a solution already exists
 
Old 05-18-2022, 03:41 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
Quote:
Originally Posted by riahc3 View Post
Why?

Over half of the things any IT person does is Google where more than 25% of the time, a solution already exists
In that case you need to pay for that activity. Looking for a solution for you.
 
Old 05-18-2022, 01:19 PM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 05-19-2022, 04:28 AM   #15
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Im already writing the script as best I can. Ill post it for feedback (or is feedback also "paid"???)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My LG Stylo 4 Has Been Hijacked by Remote Administrator and OS as Well as Kernel Have Been Modified LongDuckDong_69 Linux - Newbie 3 11-17-2019 03:41 PM
How to find a file that's modified more than 2 days ago but less than 5 days BudiKusasi Linux - Newbie 1 02-09-2018 07:25 PM
file has been modified in the last minute charleswilliams Linux - Server 1 08-25-2013 05:59 PM
[bash] Seeing if a file has been modified before/after a date w3stfa11 Programming 7 10-15-2006 06:02 PM
Get directory stats when a file has been modified? marri Programming 2 05-13-2004 08:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration