LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-23-2011, 12:16 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
Running a script


I want to run a script only when a particular command executes .. how will I achieve this ?
 
Old 08-23-2011, 06:38 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
One way of doing it would be:
1. To create a script of the same name of the command and store it in a different folder
2. Add this folder to the PATH (but before the path where the legitimate executable is stored)
3. When you will run this command it will be executed instead the command.

Ex:
We will run the script for the "cat" command
Code:
root:~# which cat
/bin/cat
root:~# cat /etc/issue
Ubuntu 10.04.2 LTS \n \l

root:~# mkdir /opt/myscript
root:~# vi /opt/myscript/cat
root:~# chmod +x /opt/myscript/cat
root:~# /opt/myscript/cat /etc/issue
Hello World!!!
Ubuntu 10.04.2 LTS \n \l

root:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
root:~# PATH=/opt/myscript:$PATH
root:~# export PATH
root:~# echo $PATH
/opt/myscript:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
root:~# cat /etc/issue
Hello World!!!
Ubuntu 10.04.2 LTS \n \l

root:~# which cat
/opt/myscript/cat
root:~#
and here is the content of my /opt/myscript/cat file:
Code:
#!/bin/bash
echo "Hello World!!!"
/bin/cat $1
Best regards,
Angel.

Last edited by angel115; 08-23-2011 at 06:43 AM.
 
1 members found this post helpful.
Old 08-23-2011, 08:28 PM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Use ps command with -C cmdlist to list only processes with program name = cmdlist.

So write a script like this:
Code:
EXECUTOR=prog_to_execute
CMDLIST=prog_to_verify_is_running
if [ $(ps -C CMDLIST | wc -l) -gt 1 ]; then
 $EXECUTOR
fi
Test it out.
 
1 members found this post helpful.
Old 08-23-2011, 11:10 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
And there's also the alias command, so you don't need to change your PATH. See man alias for details.
 
1 members found this post helpful.
Old 08-24-2011, 12:38 AM   #5
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by AnanthaP View Post
Use ps command with -C cmdlist to list only processes with program name = cmdlist.

So write a script like this:
Code:
EXECUTOR=prog_to_execute
CMDLIST=prog_to_verify_is_running
if [ $(ps -C CMDLIST | wc -l) -gt 1 ]; then
 $EXECUTOR
fi
Test it out.
This does not work in many cases as ps lists only the processes running now. And my goal was ,suppose someone restarting a service or stopping a service to catch the user.
 
Old 08-24-2011, 10:39 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,609

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by divyashree View Post
This does not work in many cases as ps lists only the processes running now. And my goal was ,suppose someone restarting a service or stopping a service to catch the user.
Stating a goal when you ask a question is always a good thing. With what you've said, you need to put sudo in place, and set users up accordingly. Sudo can be easily configured to send emails when it's run.
http://www.cyberciti.biz/faq/sudo-se...sudo-log-file/

Since you've got a built-in way of sending mail, you'll get emails when someone does something that needs root permissions, like killing/restarting a service. Taking it a step further, you can then alias your mail command to another script that you write, to parse the output, and filter what's getting sent out to only send on SPECIFIC instances.
 
Old 08-24-2011, 11:37 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Under which account should this additonal command being executed? The one of the user which started the original program, or something like a housekeeping routine running by root?
 
Old 08-24-2011, 11:31 PM   #8
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by angel115 View Post
One way of doing it would be:
1. To create a script of the same name of the command and store it in a different folder
2. Add this folder to the PATH (but before the path where the legitimate executable is stored)
3. When you will run this command it will be executed instead the command.

Ex:
We will run the script for the "cat" command
Code:
root:~# which cat
/bin/cat
root:~# cat /etc/issue
Ubuntu 10.04.2 LTS \n \l

root:~# mkdir /opt/myscript
root:~# vi /opt/myscript/cat
root:~# chmod +x /opt/myscript/cat
root:~# /opt/myscript/cat /etc/issue
Hello World!!!
Ubuntu 10.04.2 LTS \n \l

root:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
root:~# PATH=/opt/myscript:$PATH
root:~# export PATH
root:~# echo $PATH
/opt/myscript:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
root:~# cat /etc/issue
Hello World!!!
Ubuntu 10.04.2 LTS \n \l

root:~# which cat
/opt/myscript/cat
root:~#
and here is the content of my /opt/myscript/cat file:
Code:
#!/bin/bash
echo "Hello World!!!"
/bin/cat $1
Best regards,
Angel.


I used this method but the command doesnot run with its options.
 
Old 08-24-2011, 11:32 PM   #9
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by TB0ne View Post
Stating a goal when you ask a question is always a good thing. With what you've said, you need to put sudo in place, and set users up accordingly. Sudo can be easily configured to send emails when it's run.
http://www.cyberciti.biz/faq/sudo-se...sudo-log-file/

Since you've got a built-in way of sending mail, you'll get emails when someone does something that needs root permissions, like killing/restarting a service. Taking it a step further, you can then alias your mail command to another script that you write, to parse the output, and filter what's getting sent out to only send on SPECIFIC instances.
No the services are not running as root, so no one use sudo. One particular user runs the production server and everyone logged in that only.

Let me try alias.

Last edited by divyashree; 08-24-2011 at 11:42 PM.
 
Old 08-24-2011, 11:44 PM   #10
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by Reuti View Post
Under which account should this additonal command being executed? The one of the user which started the original program, or something like a housekeeping routine running by root?
One of the user started the server and he runs everything even not root.
 
Old 08-26-2011, 09:06 AM   #11
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
I was thinking of something like inotify, whenever the binary was accessed to act on this event.
 
1 members found this post helpful.
Old 08-26-2011, 10:06 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,609

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by divyashree View Post
One of the user started the server and he runs everything even not root.
Ok...so again, use sudo. Sudo does NOT have to run the root user, since (as you can do if you ARE root), you can assume any other user's ID. So, something like
Code:
sudo -u <some user ID> <some command>
would let, say, USER1 run a command as USER5, and you'd get an email. Also, if you have just 'regular' users set up to be able to start/stop system level processes without any sort of security in place, that's not a very good idea. If they're NOT system level processes, you can also just check their .bash_history files to see what they're doing.

Add to that, you can check the system logs to see when processes were stopped/started, and also do a "last" to see who logged in when, from where, and reconcile that against the system logs, so even if they remove their bash history files, you'll still have something.
 
Old 08-26-2011, 12:16 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Reuti View Post
I was thinking of something like inotify, whenever the binary was accessed to act on this event.
I had the same idea, but it's a script that needs to be run, not C. I suppose there could be a daemon written in C that executes the script. It wouldn't be particularly difficult.
Kevin Barry

edit:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/inotify.h>


int main(int argc, char *argv[])
{
        if (argc < 3)
        {
        fprintf(stderr, "%s [binary] [command...]\n", argv[0]);
        return 1;
        }


        int notify = inotify_init1(IN_CLOEXEC);
        if (notify < 0)
        {
        fprintf(stderr, "%s: can't monitor binary '%s': %s\n", argv[0], argv[1], strerror(errno));
        return 1;
        }


        int monitor = inotify_add_watch(notify, argv[1], IN_OPEN | IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF);
        if (monitor < 0)
        {
        fprintf(stderr, "%s: can't open binary '%s': %s\n", argv[0], argv[1], strerror(errno));
        return 1;
        }


        struct inotify_event events;

        while (read(notify, &events, sizeof events) == sizeof events || errno == EINTR)
        {
        if (errno == EINTR || events.wd != monitor) continue;
        if (events.mask & (IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF)) break;

        if (events.mask & IN_OPEN)
         {
        if (fork() == 0)
          {
        setpgid(0, 0);
        execvp(argv[2], argv + 2);
        _exit(1);
          }
         }
        }

        return 0;
}
Code:
> gcc main.c
> ./a.out `which ls` echo hello & #<-- monitor ls, execute echo hello
[1] 2266
> ls
hello
a.out  main.c
I tried this with monitoring a script and the script was opened twice, resulting in two "hello"s, however (probably once to id the shebang and a second time by the identified shell).

Last edited by ta0kira; 08-26-2011 at 01:14 PM.
 
Old 08-26-2011, 11:16 PM   #14
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by TB0ne View Post
Ok...so again, use sudo. Sudo does NOT have to run the root user, since (as you can do if you ARE root), you can assume any other user's ID. So, something like
Code:
sudo -u <some user ID> <some command>
would let, say, USER1 run a command as USER5, and you'd get an email. Also, if you have just 'regular' users set up to be able to start/stop system level processes without any sort of security in place, that's not a very good idea. If they're NOT system level processes, you can also just check their .bash_history files to see what they're doing.

Add to that, you can check the system logs to see when processes were stopped/started, and also do a "last" to see who logged in when, from where, and reconcile that against the system logs, so even if they remove their bash history files, you'll still have something.

Actually my concern is only for 2/3 commands. Who run them at what time, I should be informed immediately. This should not happen within production as I have to answer for that .Anytime they configure anything from GUI, they just restart the service from the commandline to load new ones.

So the mail should be fired as soon as they run the command.
 
Old 08-27-2011, 01:09 PM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,609

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by divyashree View Post
Actually my concern is only for 2/3 commands. Who run them at what time, I should be informed immediately. This should not happen within production as I have to answer for that .Anytime they configure anything from GUI, they just restart the service from the commandline to load new ones.

So the mail should be fired as soon as they run the command.
Right....AGAIN, that's what sudo does. You can allow/disallow as many commands as you'd like, and get emails when it's run.
 
  


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
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
Problem running a program/script in the background from a script newbie01.linux Programming 5 03-28-2011 07:28 AM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM

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

All times are GMT -5. The time now is 05:17 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