LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-29-2012, 01:44 PM   #1
zeke666
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Rep: Reputation: Disabled
Running "script" command from .bashrc/.cshrc


I am trying to use the "script" command to log all of the commands that the root user executes. I tried setting it up in the .bashrc file "script root-commands" where root-commands is the file that script writes to. The problem is that it starts an endless loop of the script command. Does anyone have a syntax that will start the script with only one instance. I am looking to use this so that every window opened will have it's own script file. The naming convention would be root-commands-$WINDOWID

Thanks,
Gary
 
Old 03-29-2012, 03:17 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Question, why are you doing this instead of just using the built in history recording feature of Linux? the .bash_history file in /root/ should contain all of the commands ran up to the histsize environment variable you have set on your system. You could set up a cron to rotate this log file to maintain historical data if needed.
 
1 members found this post helpful.
Old 03-29-2012, 03:42 PM   #3
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi Gary,

Welcome to LQ!!!

Kustom42 has got a point as to why you want to run script command to log everything that root does? Script not only logs the command but the output as well.

So, are you trying to capture what was the output when root executed that command? Or, you are trying to separate the commands that were used by root in different terminals? If this is the case then you have to configure the script on the basis of tty. So basically when root will open first terminal session tty will be /dev/pts/1 and the next terminal root will open it will show as /dev/pts/2. Another thing that I have noticed that you are using the command:

Code:
script root-commands
The above command is the not the correct form. If you really want to create a file then specify the file name using -f switch. So it should read as:

Code:
script -f root-commands
It will be good if you could share the script then we will be able to provide you more guidance on the basis of that.
 
Old 03-29-2012, 06:16 PM   #4
ataheri
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
same problem with script -f $(date+time).txt in bashrc running continuously

I had the same idea and I found this guide and put this command in my bashrc file. Now as soon as I log in, it starts creadting output file and I can not stop the script. Ctr+D not working and it runs the script every seconds with a new file added.
How can I access my bashrc and remove the script from there?
Thanks
==================
I found the solution:::
I was saving the log file to a folder and not the home directory. So I renamed that folder and script stopped working. so I went to bashrc and removed that line and as it is suggested here, I will check .bash_history instead

Last edited by ataheri; 03-29-2012 at 06:23 PM. Reason: find the solution
 
Old 03-29-2012, 06:20 PM   #5
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi ataheri,

Welcome to LQ!!!

You can edit your bashrc file using vi. I don't know for which user you have configured it but it was root then you will find .bashrc file under /root

You can edit it using the following command:

Code:
vi /root/.bashrc
If it was for any other user then .bashrc file will be under /home/username/.bashrc . You can edit it using:

Code:
vi /home/username/.bashrc
 
1 members found this post helpful.
Old 03-29-2012, 06:26 PM   #6
ataheri
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
Thanks for the reply. That is what I did. But after changing the folder for my log file. Before that, the script was running and I had no way to type vi or kill the script.
I wonder how could I stopped it if it was in the home directory!

Thanks anyway.
Quote:
Originally Posted by T3RM1NVT0R View Post
Hi ataheri,

Welcome to LQ!!!

You can edit your bashrc file using vi. I don't know for which user you have configured it but it was root then you will find .bashrc file under /root

You can edit it using the following command:

Code:
vi /root/.bashrc
If it was for any other user then .bashrc file will be under /home/username/.bashrc . You can edit it using:

Code:
vi /home/username/.bashrc
 
Old 03-29-2012, 06:31 PM   #7
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Did you already remove the script line from your .bashrc file? If not then remove it, logout and logback in the system and it will not run next time. You cannot stop the script using ctrl+D because you have started it in the background. So easiest way will be log off and then log back in.

Another way to stop the script without logging off and logging back in is to find out the pid of script and kill it. You can use the following command:

Code:
ps -eaf | grep script
This will give you the pid of script. Remember the first column lists the pid. Once you know the pid you can kill that pid using the following command:

Code:
kill -9 <pid_number_found_from_previous_step>
I was giving an example that if you have set this up for root then you have to edit /root/.bashrc and if you have set it up for any other user then the location of that file will be /home/username/.bashrc
 
Old 03-29-2012, 06:57 PM   #8
ataheri
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
That is what I did. I removed the script line from bashrc and updated the shell without logout using this command:
source ./.bashrc
My problem was that it was giving a message that an scrpit started with the file name and the time it was created. since I was given time as a file name $(date +"%b-%d-%y")-log.txt, it was creating this logfile every second and I had no time of typing in the shell. even typing it outside and pasting it in the shell wasn't helpful.
 
Old 03-29-2012, 07:34 PM   #9
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Tell me this, is the issue still exist? I don't think so it does as far as I can understand from your post. You want to know why it went into the loop and why it was continously displaying those lines. Is it? If that is the question then as per my testing it appears that as soon as .bashrc calls the script the script command inturn calls .bashrc again and thus resulting in endless loop. This loop can be terminated using ctrl+c but putting the script command in .bashrc is obviously not a right choice.

If you want to automate script command then I would say better place would be .bash_profile file. If you put it the following way in .bash_profile then it will work smoothly:

I am assuming that this is for root

1.
Code:
vi /root/.bash_profile
2. In .bash_profile put the following line at the end:

Code:
script -f logscript.txt
Additional information: Once you will login it will say "Script started, file is logscript.txt". You can give any name to the file you want to login. If you want to stop the script just type "exit" without quotes at the console and it will stop logging it. You will see a message: "Script done, file is logscript.txt"

Last edited by T3RM1NVT0R; 03-29-2012 at 07:38 PM. Reason: Additonal information
 
Old 03-29-2012, 08:56 PM   #10
ataheri
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
That is exactly what I wanted to say. Thank you for the description as well.
 
Old 03-30-2012, 05:28 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
actually I do not think it is a good idea to put script into *rc or bash_profile since this command will break the normal login process and forks an additional shell (- and when you exits that shell will the normal login process completed). In general starting a new shell in *rc may cause an endless loop, because the child shell will process that *rc file again.
I saw here source ./.bashrc which is not ok again. The source command is available in tcsh, but .bashrc is used by bash, not tcsh.
 
Old 03-30-2012, 10:51 AM   #12
ataheri
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
So do you think using source ./.source might cause problem as well? Because I am using it whenever I am adding an alias or export path to my bashrc file to upldate my session without the need to log-out and log-in again.
 
Old 03-30-2012, 11:12 AM   #13
zeke666
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
As always I overlooked the obvious. The bash_history will work just fine. I'll have to work out a rotation system that renames it with date and stores it in another location....Thanks All.
 
Old 03-30-2012, 12:57 PM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by ataheri View Post
So do you think using source ./.source might cause problem as well?
I assume you would write source ./.bashrc or something similar.

In unix/linux you can do it, just it is not usual. If you never uses bash you will never need .bashrc, so you can do it. The conflict will occur when you want to use both (bash and tcsh), because they have different syntax. Therefore a file named .bashrc can be used as planned with the original syntax, you can use it with tcsh or you can even store a word document in it (or use for anything you want)
 
Old 03-30-2012, 02:16 PM   #15
zeke666
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Just to be clear... I put "script filename" in my .bashrc so that any new window that was opened would kick off a script file. It would launch about 100 script commands mostly to the same 4 or 5 files it had created. I now think using the bash_history file will cover what I am looking to do.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Invalid Command" error running .sh script nixanwin Linux - Newbie 4 07-18-2011 10:49 AM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
why to commit .cshrc "source .cshrc" file in every new shell session simer_anand88 Mandriva 3 10-23-2009 09:44 AM
Running command in gnome-terminal from script results in "...error creating child..." PlancksCnst Linux - General 6 04-08-2009 12:09 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:54 AM.

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