LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2020, 02:43 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Slackware, Amazon Linux
Posts: 2,028
Blog Entries: 20

Rep: Reputation: 128Reputation: 128
every hour run a bash function inside a bash shell


i know how to define bash functions. i'd like to have a bash function run inside a bash process every hour. well, maybe every 20 minutes. too bad cron is a separate process.
 
Old 05-25-2020, 03:20 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,873

Rep: Reputation: 317Reputation: 317Reputation: 317Reputation: 317
I suggest that you run a continuous loop with a sleep function in it. See:

man sleep

-----------------
Steve Stites
 
2 members found this post helpful.
Old 05-25-2020, 04:54 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Slackware, Amazon Linux
Posts: 2,028

Original Poster
Blog Entries: 20

Rep: Reputation: 128Reputation: 128
that would block the shell continuously. i want to limit blocking the shell to the time it takes for the function to run.
 
Old 05-25-2020, 05:56 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,571

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
You could tell the scheduled other process to signal your running process, and call the function you want from a signal handler.

https://www.gnu.org/software/bash/ma...tml#index-trap
 
1 members found this post helpful.
Old 05-25-2020, 08:43 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,774

Rep: Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537Reputation: 2537
It'd be good to know exactly what you are trying to achieve and why.
There may be a lateral soln.
 
2 members found this post helpful.
Old 05-25-2020, 08:53 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,697
Blog Entries: 13

Rep: Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915Reputation: 3915
Quote:
Originally Posted by Skaperen View Post
that would block the shell continuously.
I don't agree. It's a process. Doesn't starve the system.
 
2 members found this post helpful.
Old 05-25-2020, 09:23 PM   #7
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,331

Rep: Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432
Quote:
Originally Posted by Skaperen View Post
i know how to define bash functions. i'd like to have a bash function run inside a bash process every hour. well, maybe every 20 minutes. too bad cron is a separate process.
If I understand you correctly, you want to create a bash script to replicate the functionality of cron or at. Why would you want to do that? What advantage obtains?
 
Old 05-26-2020, 08:57 PM   #8
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Slackware, Amazon Linux
Posts: 2,028

Original Poster
Blog Entries: 20

Rep: Reputation: 128Reputation: 128
i want to routinely check the environment variables for change in my interactive shell. once an hour or so is good for now.
 
Old 05-26-2020, 10:32 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,422

Rep: Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027
The environment variables within a process will never change from their inherited values unless that process itself changes them. Within an interactive shell, the only way to change them would be by a command line direct assignment or by a sourced (not simply executed) script. There is no notion of some "global environment" that other processes can chamge at will and have those changes seen by all.

Last edited by rknichols; 05-26-2020 at 10:34 PM.
 
3 members found this post helpful.
Old 05-27-2020, 02:22 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,618

Rep: Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755Reputation: 4755
Quote:
Originally Posted by Skaperen View Post
i want to routinely check the environment variables for change in my interactive shell. once an hour or so is good for now.
Why? And what should be the result?

In bash you can call a function in PS1 too (although it is not efficient and not really practical).
 
Old 05-27-2020, 06:42 AM   #11
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,331

Rep: Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432
To do that you might have to get a bit fancy. It is not a function running form a script that you want. You want something like a function or script running from each new display of the default command line prompt, or that you manually call on occasion. There is no other way for it to have access to the environment you are running in that for you to run it in your session somehow. (Otherwise it gets it's own environment, which may be different from the one you want to monitor.)
 
Old 05-27-2020, 06:32 PM   #12
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Slackware, Amazon Linux
Posts: 2,028

Original Poster
Blog Entries: 20

Rep: Reputation: 128Reputation: 128
there are a couple new environment variables showing up that i do not recognize (any of my shell functions exporting). i have grepped every file for their names and nothing is found. these are not getting set when the shell first starts up (or i could blame myown .bashrc file). so they must be getting set later. by being alerted to this, my command history might still show me the possibilities to let me eventually narrow down the culprit. i'm concerned about these variables because they resemble some confidential/personal data. they are not exact, but close enough to raise eyebrows.

i do use lots of shell functions, many in lieu of aliases, many longer.
 
Old 05-27-2020, 09:42 PM   #13
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Slackware, Amazon Linux
Posts: 2,028

Original Poster
Blog Entries: 20

Rep: Reputation: 128Reputation: 128
got a solution. save the shell PID in a file [echo $$ >shell.pid]. cron a script that gets the PID from that file [if it does not exist, just quietly exit]. then it grabs the environment variables and saves them [tr '\0' '\012' </proc/${pid}/environ|grep =|sort >/home/phil/$(date +%Y_%m%d_%H%M)] in a unique file. then i can scan them later. the next version would look for those names and if it sees them, send me a message.
 
Old 05-28-2020, 05:00 AM   #14
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,920

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by Skaperen View Post
got a solution. save the shell PID in a file [echo $$ >shell.pid]. cron a script that gets the PID from that file [if it does not exist, just quietly exit]. then it grabs the environment variables and saves them [tr '\0' '\012' </proc/${pid}/environ|grep =|sort >/home/phil/$(date +%Y_%m%d_%H%M)] in a unique file. then i can scan them later. the next version would look for those names and if it sees them, send me a message.
I was going to suggest something very similar: periodically grabbing the contents of /proc/$$/environ and saving it in a file, say, "my.env". Then wake up some time later, rename "my.env.prev" and grab the environment, again, into "my.env"). Then use diff(1), md5sum(1), or whatever you like to detect a change between the two snapshots, squawking if a change is seen---perhaps using diff(1) and emailing yourself the changes, repeating ad nauseum. But... when I try that: grabbing a snapshot, defining a new environment variable, and grabbing another snapshot, diff(1) shows no difference between the two snapshots. Hmm...

Back to the drawing board...
 
Old 05-28-2020, 05:04 AM   #15
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,920

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by wpeckham View Post
To do that you might have to get a bit fancy. It is not a function running form a script that you want. You want something like a function or script running from each new display of the default command line prompt, or that you manually call on occasion. There is no other way for it to have access to the environment you are running in that for you to run it in your session somehow. (Otherwise it gets it's own environment, which may be different from the one you want to monitor.)
You could kick the script off, passing your PID to it, and run it in the background, via "at", via nohup (maybe) and grab the environment out of /proc/<PID>/environ. However, I tried that and saw no differences after making a change to the environment (see other reply: #14).

Last edited by rnturn; 05-28-2020 at 05:07 AM. Reason: Added post number
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] shell script to check availability of a file for 1 hour , after one hour if file not there , it will echo "Time Out" subir Linux - Newbie 4 03-10-2020 07:05 AM
Schedule php script every hour, half an hour, and 15 minutes manolakis Programming 5 08-22-2018 07:43 AM
[SOLVED] How to: Script run every 1 hour between 18h and 7h every day heathcliffz Linux - General 5 12-08-2015 09:02 AM
Cron job issue - every hour works, but specific hour fails lunarleviathan Linux - Newbie 6 11-20-2009 12:19 AM
Aergh. X dies on the hour, every hour l00zer Linux - Software 4 06-07-2005 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration