script command - bash linux terminal - linux

I am running the cmd
script install-log.txt
the terminal successfully returns
Script started, file is install-log.txt
If I begin typing commands and receiving output to the screen
lsblk
fdisk -l
ls
echo ok
when I check the install-log.txt
nano install-log.txt
it is empty.
I thought all cmd was supposed to be saved there until the session is finished?
I am using Arch-Linux installation CD, and wanted to save this log to record my installation setup cmds.

You need to terminate script operation by running 'exit' command. That wont exit your terminal as such. Then you can view your log file.
Here is the duplicate with more detailed info -> Bash script: Using "script" command from a bash script for logging a session

Related

Execute bash script on a remote-linux

I have a bash script lets say test.sh. This script contains the following:
#!/bin/bash
echo "khaled"
ads2 svcd&
This script simply prints my name (just for test purposes) and execute ads-service application in the background. When i run the script on my ubuntu, it works correctly. As a test i checked which programs run on the kernel
As you see. ads2 runs and has 12319 process-id.
Now what I'm trying to do is to run the script on the ubuntu, however remotely from a windows pc.
Therefore i opened command-line on windows and executed the following command:
ssh nvidia#ubuntu ip-address ~/test.sh
And i get the following
As you see the scripts run and prints khaled,however on windows command line and what i want is that the script is executed on the ubuntu. this justify why the lineads2 svcd& doe not do anything, neither on windows (which makes sense, since ads2 is installed on ubuntu) nor on linux.
So how can i execute the script on ubuntu ?
thanks in advance
Use the full path to start ads2. When using remote SSH your environment variables may be different than in a local shell.
#!/bin/bash
echo "khaled"
/home/nvidia/ads2 svcd&
Not sure where ads2 is located.
Try the following to locate it on your Ubuntu local shell.
command -v ads2
You may also need nohup to persist the process beyond the life of the SSH session.
If you have the script on the remote server and you want to run this, you would add back ticks,
ssh user#server './test/file.sh'
The script's output would be sent to your local machine, as if you ran the command from your local machine.

bash script to auto run on boot, make screen, execute a command and detach

I am using Centos 7 and on boot I would like to:
Make a screen
Execute a command: osrm-routed --algorithm=MLD
~/osrm-backend/profiles/australia-latest.osrm
Detatch from screen (possibly not needed, just as long as I can
access it myself after its running in future)
Here is something I have thought about, although not correct and wont work
filename: mapstart.sh
Contents of file:
#!/bin/bash
/usr/bin/screen -dmS mapapi osrm-routed --algorithm=MLD ~/osrm-backend/profiles/australia-latest.osrm
With your help with the script. I am not sure the best way to run that on boot with centos 7.
Appreciate your help and input.
For those who would like to know. The issue was with OSRM and centos. I was able to get it running using the full paths of everything and the following in crontab -e
To get the full path of osrm-backend i ran the command of:
which osrm-routed
It returned the result of:
/usr/local/bin/osrm-routed
That then enabled me to add the full path of the command I was trying to run from crontab -e which is required. From there it worked running the below in crontab -e
#reboot /usr/bin/screen -dm -S pistartup /usr/local/bin/osrm-routed --algorithm=MLD ~/osrm-backend/profiles/australia-latest.osrm
break down of all the above:
runs command at reboot only:
#reboot
full path to screen command:
/usr/bin/screen
create screen with name of pistartup and detach:
-dm -S pistartup
my particular command i wanted to run inside the screen:
/usr/local/bin/osrm-routed --algorithm=MLD ~/osrm-backend/profiles/australia-latest.osrm
Now when ever the machine is rebooted. it has created a screen and run my command. To resume the screen manually If i ever wanted to, i could issue the command of:
screen -r pistartup

Jenkins : bash script ran with nohup is neither working nor writing anything to log

BACKGROUND
I would like to explain the scenario properly here.
I am running jenkins_2.73.3 in my cloud server with ubuntu 16.04.
Currently, there are 3 users in the server:
root
develop-user (which I had created for many reasons such as test,deploy etc)
jenkins (which was created by jenkins ofcourse, I also added this jenkins user to sudoers group)
PROBLEM
I have a bash script that I am calling from a build step in Jenkins. Within this bash script,there is a nohup command for calling a separate deployScript in the background such as:
#!/bin/bash
nohup deployScript.sh > $WORKSPACE/app.log 2>&1 & echo $! > save_pid.txt
After the build step is completed, I see that a id is generated inside save_pid.txt but app.log is surprisingly empty. I can't kill any processes with this generated pid. So, that means there isn't any process created in the first place here. Also, the deployScript.sh does not seem to have any effect at all. It's just not working. This happens everytime I run the build in Jenkins. I can assure that there is nothing wrong with the deployScript.sh.
I have tried running this bash script with the develop-user manually without Jenkins and it works perfectly. Contents are written to the log file and also I can use the generated pid to kill the process. I have also tested this in my local environment and it works.
QUESTION
I have been looking at this for days. What might be the root cause here ?Where can I look into to see some logs or other info ? How is the pid generated whereas the log file is empty ? Is it a permission issue with the jenkins user ? Please help.
You can use below line inside the execute shell in jenkins to run it in background without the process being killed.
BUILD_ID=dontKillMe <command> &
So, it turned out to be a permission issue and also the script wasn't executable I guess as pointed out in the comments above.
So, now the bash script looks like below:
#!/bin/bash
sudo chmod a+x deployScript.sh
sudo nohup deployScript.sh > $WORKSPACE/app.log 2>&1 & echo $! > save_pid.txt
This works.

Obsidian scheduler for running linux scripts

I am using obsidian scheduler for scheduling various jobs written on a linux box. And trying to call shell scripts with a nohup command like
UPDATE 1:
nohup ./script.sh > output.txt &
UPDATE 2
This is the error when i use nohup.
nohup: failed to run command â./test.sh &>./load.log &â: No such file or directory
I dont see anything writing to the output file.
And secondly how can i verify that it is using nohup command to execute the script.
Thanks

Can we save the execution log when we run a command using PuTTY/Plink

I am using Plink to run a command on remote machine. In order to fully automate the process I
need to save the execution log somewhere. I am using a bat file:
C:\Ptty\plink.exe root#<IP> -pw <password> -m C:\Ptty\LaunchFile.txt
The C:\Ptty\LaunchFile.txt contains my command that i want to run.
./Launch.sh jobName=<job name> restart.mode=false
Is there a way to save the execution log so that I can monitor it later... ?
The plink is a console application. Actually that's probably it's only purpose. As such, its output can be redirected to a file as with any other command-line command/tool.
Following example redirects both standard and error output to a file output.log:
plink.exe -m script.txt username#example.com > output.log 2>&1
See also Redirect Windows cmd stdout and stderr to a single file.
This is the one of my way to log everything when I use putty.exe on Windows.

Resources