Copy all typed command in a linux console & their result to a file - linux

I'm trying to make a script to automatically install programs and configure them on my Fedora 19 linux distribution.
To create it, I made a VM and I'm typing all the command manually in my "Terminal" application.
I'd like to be able to log all what I've typed and all the output (stdin & stderr & stdout if I understood it well) so I can use this log to make my script.
Is there a way to do this ?

You can use the script command to record your session:
$ script session.txt
Script started, file is session.txt
$ ls
session.txt
$ exit
Script done, file is session.txt
$ cat session.txt
Script started on Wed 31 Jul 2013 07:36:40 AM CEST
$ ls
session.txt
$ exit
Script done on Wed 31 Jul 2013 07:36:42 AM CEST

Related

'command not found' when trying to run bash script [duplicate]

This question already has answers here:
Shell script not running, command not found
(12 answers)
Closed 3 years ago.
I am trying to run a bash script from a script called dev_ro, here is how it's being called.
export SUBNET="$(first_available_docker_network --lock-seconds 7200)"
I am calling dev_ro by ./dev_ro
I am confirm I have
#!/bin/bash
at the top of both files.
Here are perms for both files
$ ls -lh dev_ro
-rwxrwxr-x 1 ME ME 423 Aug 21 15:57 dev_ro
$ ls -lh first_available_docker_network
-rwxrwxr-x 1 ME ME 2.2K Aug 21 15:55 first_available_docker_network
This is the output from running ./dev_ro
++ first_available_docker_network --lock-seconds 7200
compose/everest-compose: line 25: first_available_docker_network: command not found
Additionally when I try to run the script:
ME#SERVER:~/Rosetta/compose$ first_available_docker_network
first_available_docker_network: command not found
ME#SERVER:~/Rosetta/compose$
I have the same setup running on a different server and it's working. The code was pulled from Git, so it's the same codebase.
Any help is much appreciated.
ME#OTHER_SERVER:~/Rosetta/compose$ first_available_docker_network
DEBUG:root:Docker subnets: [IPv4Network(... etc
ME#OTHER_SERVER:~/Rosetta/compose$ ^C
first_available_docker_network is not a standard linux command. This must be your custom script. Try executing using its absolute path. For example, instead of using+
ME#SERVER:~/Rosetta/compose$ first_available_docker_network
use
ME#SERVER:~/Rosetta/compose$ absolute_path_of_script/first_available_docker_network
Or alternatively,
You can try adding the path of first_available_docker_network to the PATH variable.

How to distinguish between two running Linux scripts with the same name?

I use SSH to connect to Linux, maybe run a Linux script multiple times, and use nohup to suspend these processes, and then close the SSH connection. After the next SSH connection to Linux, how can I distinguish between different scripts and get different PIDs?
This Linux script will always print content on the screen. I use Python's paramiko library, SSH to Linux, run the script and use nohup to suspend the process and redirect the output to the file. This process may be multiple times. How to distinguish the starting process, find its PID and kill it. It is best not to modify the Linux script because the script is not written by me.
I use the script name to find the process number, get a lot of PIDs, I can't distinguish them.
You could parse the output of ps -eo pid,lstart,cmd which shows the process id, start time and path, e.g.:
PID STARTED CMD
1 Mon Jun 19 21:31:08 2017 /sbin/init
2 Mon Jun 19 21:31:08 2017 [kthreadd]
3 Mon Jun 19 21:31:08 2017 [ksoftirqd/0]
== Edit ==
Be aware that if the remote is macOS the ps command does not recognize the cmd keyword, use comm or command instead, e.g.: ps -eo pid,lstart,comm
Use ps command to check running process.
For checking only shell scripts , You an do something like this:-
ps -eaf |grep .sh
This will give you all the information about running shell scripts only, easily you can distinguish b/w running scripts.
In place of ".sh" you can give file name also, than you will get information only about that running file.
maybe change the command you run to do something like:
nohup command.sh &
echo "$! `date`" >> runlog.txt
wait
i.e. run the command in the background, append its PID to a log (you might want to include more identifying information here or use a format that's more easily machine readable), then wait for it to finish
another variant would be to run tmux (GNU screen) on the server and run commands in an existing session:
tmux new-window command
which would also let you "reconnect" to running scripts later to check output / terminate

Run script on Startup on openSUSE

Task: Run chromium on startup on openSUSE
So far:
First I don't know what path to take, it's possible with Cron or from rc.local. I don't know which opinion is the best
Cron:
Figured out that it's not a very good idea
rc.local
So I have this script:
Fri Aug 11; 06:10:38; marton;/etc/init.d ; $ cat /etc/init.d/chrom_start.sh
#!/bin/bash
/usr/lib64/chromium/chromium
exit 0
I have permissions for the file:
Fri Aug 11; 06:11:09; marton;/etc/init.d ; $ ls -l /etc/init.d/chrom_start.sh
-rwxrwxr-x 1 root root 48 Aug 11 06:10 /etc/init.d/chrom_start.sh
openSUSE doesn't have update-rc.d
Fri Aug 11; 06:12:48; marton;/etc/init.d ; $ update-rc.d
If 'update-rc.d' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf update-rc.d
I can't seem to find the example:
Fri Aug 11; 06:13:18; marton;/etc/init.d ; $ cat /etc/init.d/skeleton
cat: /etc/init.d/skeleton: No such file or directory
Somewhere I found that I have to use install job but it does not exist
Fri Aug 11; 06:20:35; marton;/etc/init.d ; $ %install
bash: fg: %install: no such job
So, if everything is alright, I just have to find a way to set the daemon to run on startup, what do I do next, considering that I don't have this skeleton file and these install job does not exist?
I don't know if it'll be of help on OpenSUSE, but here is a thread about "how to autostart Chromium" :
https://raspberrypi.stackexchange.com/questions/38515/auto-start-chromium-on-raspbian-jessie-11-2015
This may help too.
If you are using bash (it is the most probable scenario) you could edit your .bash_profile file, or .bash_login (both should be in your user's home directory).
You need to add at the end your chromium's path like:
/usr/bin/chromium&
the final & is to make it run as a background process. If you want to apply this change for every user you can edit your .bash_login file on /etc/skel
Hope it helps,
ps. here is an url that may help a bit.
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html

Output shell script result to file like terminal screen

I am a beginner in shell scripting and this is my sample script:
#!/bin/ksh
echo "start script"
ls
echo "end script"
I run it by this command: ./runScript.ksh > outPutFile.txt
and got the output inside file:
start script
file1.txt
file2.txt
file3.txt
end script
I just want to ask if it is possible to have this output on the file? Like what is shown if the commands are executed on the terminal?:
user#serverName:/parentPath/childPath/>echo "start script"
user#serverName:/parentPath/childPath/>ls
file1.txt
file2.txt
file3.txt
user#serverName:/parentPath/childPath/>echo "end script"
Thank you so much in advance. I will really appreciate any help.
Use the 'script' command. Any command issued after the 'scipt' command will be captured in the specified file (or default file typescript).
**Example:**
$ script my_output.txt
Script started, output file is my_output.txt
$ uname -r
16.1.0
$ date
Wed Mar 8 12:55:29 IST 2017
$ exit
Script done, output file is my_output.txt
$ cat my_output.txt
Script started on Wed Mar 8 12:55:19 2017
$ uname -r
16.1.0
$ date
Wed Mar 8 12:55:29 IST 2017
$ exit
Script done on Wed Mar 8 12:55:32 2017
$
man script
SCRIPT(1) BSD General Commands Manual SCRIPT(1)
NAME
script -- make typescript of terminal session
SYNOPSIS
script [-adkpqr] [-F pipe] [-t time] [file [command ...]]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session
as proof of an assignment, as the typescript file can be printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.

shell script to capture keyboard activities

How to capture all keyboard strokes using shell script .Is there any command that is related to keyboard activities.
If you would like to log all input and output, you can use the script command.
$ script transcript.txt
Script started, file is transcript.txt
$ echo 'Hello, world!'
Hello, world!
$ exit
Script done, file is transcript.txt
$ cat transcript.txt
Script started on Thu 09 Sep 2010 03:06:56 PM EDT
$ echo 'Hello, world!'
Hello, world!
$ exit
Script done on Thu 09 Sep 2010 03:07:06 PM EDT
Check out the trap command.
For example, type in your console :
trap "echo \"Arrrrggghhhh\"" INT
Now press Ctrl + C - fun fun :)

Resources