How to execute a script on linux server by windows client - linux

I have windows clients working on a linux server.
There are batch files to start the programs.
The program itself is a windows executable. The path comes from a samba share and the executable starts under the users login automatically.
The Batch looks like
set lw=A:
if not exists %lw% goto connect
goto start_prog
:connect
NET USE %lw% /delete
NET USE %lw% \\server\progpath
goto start_prog
:start_prog
set XXX=YYY
...
start %lw1%\...\prog.exe -X options
echo ....
So far there is no problem, but to cleanup some files, I would like to delete some configuration data before starting the exe. This can manually be done by
sudo find /... -type f -name ...<userlogin>... -exec rm {} ';'
But how to start such a script by the batchfile?
Where does the shell script gets the username from?
And how to start it as root/sudo?
Server:
Ubuntu, Release 18.04
Clients:
Windows goes from Win10 to Win11

Related

Perform bash command after connecting to Ubuntu server (Windows 10)

I'm trying to make a simple Windows batch script that will make my life easier, but i don't know if it's quite possible. It basically automatically connect to the Ubuntu server using SSH. But when i enter the server, none of my commands work (as you would expect, since is a different terminal). However, i made a shell script and placed in the root directory of the server (ubuntu#ip-182-74-3-112:~$) that performs the actions, i just need to execute this script automatically when i connect to the Ubuntu server. Is it possible?
Batch script:
cd "C:\Users\myUser\Documents"
ssh -i .\my_server.pem ubuntu#www.website.com.br
// ./restart.sh (doesn't work)
Shell script:
#!/bin/bash
cd "server/glassfish4/glassfish/bin/"
for i in 1 2 3; do
sudo ./asadmin stop-domain domain1
done
for i in 1 2; do
sudo ./asadmin start-domain domain1
done
The answer was so simple that didn't come to my mind. I just changed
cd "C:\Users\myUser\Documents"
ssh -i .\my_server.pem ubuntu#www.website.com.br
to
cd "C:\Users\myUser\Documents"
ssh -i .\my_server.pem ubuntu#www.website.com.br "./restart.sh; bash"
Ignore my question, i'm just leaving here for convenience

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.

How to remotely run a (shebang prefixed) node script using ssh?

I want to remotely run a node.js script containing a shebang line through ssh, similarly as when running it locally.
myscript file:
#!/usr/bin/env node
var param = process.argv[2] || 'help';
//... other js code
When running locally on each host – e.g. myscript arg1 – it runs successfully. When running remotely on a "sister" node in a cluster (containing the same file and directory structure, including nodeand myscript):
ssh -o "PasswordAuthentication no" bob#123.1.2.3 /path/to/myscript arg1
I get /usr/bin/env: ‘node’: No such file or directory error.
Am I missing a ssh param / option?
Mode details: If I run
ssh -o "PasswordAuthentication no" bob#123.1.2.3 echo "hello"
It also works fine. Forgive me it this is obvious to you, I'm not an advanced Linux user, the ssh manual seemed a little bit overwhelming and tried a couple answers found here with no success:
What exactly does "/usr/bin/env node" do at the beginning of node files?
Run scripts remotely via SSH
how to run a script file remotely using ssh
If the node executable isn't already in your PATH environment variable at login, you could provide the full path to it in the shebang line of your script:
#!/usr/bin/env /full/path/to/node
As others have commented, you would have to update your script if the path to node ever changes. This is not ideal. Alternatively, you could force ssh to create a pseudo-terminal session by specifying the -t flag and run your script in an interactive bash shell:
ssh -t -o "PasswordAuthentication no" bob#123.1.2.3 'bash -ic "/path/to/myscript arg1"'
Sebastian's answer inspired me to find a solution that doesn't hardcode the full path to node on the script. Basically, I make sure the remote PATH is available before running the command:
ssh -o "PasswordAuthentication no" bob#123.1.2.3 "export PATH=$PATH;/path/to/myscript arg1"
But this only worked for me because both local and remote servers have the same PATH value, since the local PATH is being set onto the remote session.
Here there may be some ways to explore other solutions if your case is not like mine:
How do I set $PATH such that `ssh user#host command` works?
How to set PATH when running a ssh command?

Execute commands on linux remotely through Dropbox ?

I am working in a large lab with linux machines and we are using it for doing CGI stuff. Basically, I want to be able to execute commands on the machine that I am logged in there, while I am at home (using Windows here). So far I've been able to get the output of the terminal to be written in realtime on a txt file which is saved on dropbox, so I can check the progress of my processes while I am at home. So I am thinking about a way of reversing the process. Is it possible to save the commands in a txt or sh file on dropbox and have a process on my machine at the labs which is constantly looking at this file and executing the commands written there ?
Install inotify-tools
Ubuntu:
sudo apt-get install inotify-tools
Code
inotifywait -m ~/Dropbox -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
# do something with the file
done
This works for me

how to start the tomcat server in linux?

i tried to install
1.yum install -zxvf apache-tomcat-6.0.47.tar.gz then
2. export TOMCAT_HOME=/home/mpatil/softwares/apache-tomcat-6.0.37
3. [root#localhost mpatil]# echo $TOMCAT_HOME
/home/mpatil/softwares/apache-tomcat-7.0.47
while starting tomcat by using this command
4.[root#localhost mpatil]# /startup.sh
bash: /startup.sh: No such file or directory
i don't know why it showing like this.
my file in
5.[root#localhost mpatil]# find /home -type f -name apache-tomcat-6.0.37.tar.gz
/home/mpatil/Downloads/apache-tomcat-6.0.37.tar.gz
what i tried before this is is correct or not? --please tell me
my question is how to start a tomcat server in linux.Please tell me..
The command you have typed is /startup.sh, if you have to start a shell script you have to fire the command as shown below:
$ cd /home/mpatil/softwares/apache-tomcat-7.0.47/bin
$ sh startup.sh
or
$ ./startup.sh
Please try that, you also have to go to your tomcat's bin-folder (by using the cd-command) to execute this shell script. In your case this is /home/mpatil/softwares/apache-tomcat-7.0.47/bin.
Use ./catalina.sh start to start Tomcat. Do ./catalina.sh to get the usage.
I am using apache-tomcat-6.0.36.
if you are a sudo user i mean if you got sudo access:
sudo sh startup.sh
otherwise:
sh startup.sh
But things is that you have to be on the bin directory of your server like
cd /home/nanofaroque/servers/apache-tomcat-7.0.47/bin
I know this is old question, but this command helped me!
Go to your Tomcat Directory
Just type this command in your terminal:
./catalina.sh start
cd apache-tomcat-6.0.43 ====: Go to Tomcat Directory
sh bin/startup.sh =====: Start the tomcat on Linux
sh bin/shutdown.sh ======:Shut Down the tomcat on Linux
tail -f logs/catelina.out ====: Check the logs
Go to your Tomcat Directory with : cd/home/user/apache-tomcat6.0
sh bin/startup.sh.>> tail -f logs/catelina.out.>>
Go to the appropriate subdirectory of the EDQP Tomcat installation directory. The default directories are:
On Linux: /opt/server/tomcat/bin
On Windows: c:\server\tomcat\bin
Run the startup command:
On Linux: ./startup.sh
On Windows: % startup.bat
Run the shutdown command:
On Linux: ./shutdown.sh
On Windows: % shutdown.bat
To run Apache Tomcat server in Linux following ways can be used. Let me share the difference as well since I see some got confusions with the multiple ways Apache Tomcat Server can be started.
The catalina.sh is the main control script for Tomcat. Following are the multiple ways of running tomcat:
./catalina.sh run
Passing "run" argument for catalina.sh --> starts the Tomcat in the foreground and displays the running logs in the same console. when the console terminal is closed it will terminate the tomcat.
./catalina.sh start
Passing "start" argument for catalina.sh --> starts the Tomcat in the background. Since in background no issues closing down the terminal. The logs need to be viewed as below: tail -f $CATALINA_HOME/logs/catalina.out
./startup.sh
The last way is firing the startup.sh to start your Tomcat server. If you Vi the script you can see it calls catalina.sh script passing start as the argument. This will be running in background as well.

Resources