Linux - GeneralThis 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
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.
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
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.
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.
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
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
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:
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
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.
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
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.
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.
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.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.