I'm running a Half-Life dedicated server on my VPS with following bash file named startserver in HLDS:
#!/bin/bash
screen -A -m -d -S hlds ./hlds_run -console -game valve -ip **ip address** -port 27015 +maxplayers 16 +map crossfire > /dev/null >&1 &
So I type ./startserver and my server is running. I'm not owner of this code, I just copied it. And I tried to make this code work for a PHP file and I created a directory named "test" and I have "test.php" code so that I can run my php file by typing "php test.php" but I need to run it on a screen too, because I'm not always gonna run the putty client.
I tried to edit the code in the directory where my php file exists and that's what I got inside:
#!/bin/bash screen -AmdS phper php test.php > test.txt >&1 &
I supposed to create a screen named "phper" and run the test.php inside. All files exist in the directory but I face with that error:
/bin/bash: screen -AmdS phper php test.php > test.txt >&1 &: No such file or directory
Am I doing something wrong?
The first line is wrong, if it is one line
#!/bin/bash screen -AmdS phper php test.php > test.txt >&1 &
First line of text file with executable bit set may begin with "#!" to set interpreter of the file, it is called Shebang. When you run the script and there is shebang, linux kernel will recognize it and start interpreter with the name of script as its argument.
You should not add anything after "/bin/bash", or it will try to parse it as his own parameters, and there is no bash parameter "screen" and no file with name "screen" (and with bash script inside) in current directory.
So, try put bash in shebang on first line and actual bash script from second line:
#!/bin/bash
screen -AmdS phper php test.php > test.txt >&1 &
Related
I am trying to use a shell program that I have been left to get running. The program starts as shown below.
#!/usr/bin/env bash
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$1/logs/$(date '+%Y%m%d_%H%M%S')_start.log 2>&1
The program is a .sh with the input ($1) being the output folder such that it is looking in /output/logs..., the program fails on the exec line, this is my first time using bash and shell scripting but I think this line essentially redirects all the standard outputs to the log file?
The program error say
cannot create /output/logs/20230203_12345_start.log: directory non-existent
should this line also create the log file if it is non existent? I don't see how you could create the .log file first as otherwise you would get the seconds part of the title wrong?
It's not complaining about the file name, it says directory non-existent. I/O redirection can't make directories. Create it first with mkdir -p "$1"/logs, then your line should work.
Why can't I run a bash script in the current directory I'm in?
Whenever I run the script the commands are executed in the home directory.
The only answers I found are included below.
I do use the zsh shell. I don't know if that changes anything.
Thanks in advance!
What I have tried so far:
#!/bin/bash
touch test.txt
#!/bin/bash
cd $PWD
touch test.txt
#!/bin/bash
variable = $PWD
cd $variable
touch test.txt
#!/bin/bash
variable= pwd
cd $variable
touch test.txt
#!/bin/bash
cd -
touch test.txt
If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.
in bash there are two things to do:
ensure that the shell script file is saved properly and is chmod'd to be an executable.
To do so, save the file (e.g. script.sh) with the code you want, and then run chmod +x script.sh to make linux understand that this file is an executable.
call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).
This should execute the file. from there, it's about your code and if you need help there, please update your question.
I am trying to run a shell script located on a Linux server from Windows. The shell script does two things:
Do a sed command to replace text in an .sql file in the same directory.
Run the .sql file with sqlplus.
The shell script:
!/bin/sh
arg1=$1
arg2=$2
arg3=$(echo $arg1 | tr '[:lower:]' '[:upper:]')
arg4=$(echo $arg2 | tr '[:lower:]' '[:upper:]')
echo $arg1
echo $arg2
echo $arg3
echo $arg4
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
sqlplus $arg2/$arg2#MYDB <<EOF
#sequence.$arg1.sql
exit;
(My database is located on the same Linux server.)
1) Script runs correctly when I log in to the server via MobaXterm
Connect to server with userID.
Set my_env.
cd to the shell script's directory.
Run script with ./myscript.sh with arguments.
2) Same shell script runs successfully via .cmd manually
Create a Windows script test.cmd on my Windows PC.
In the .cmd file I have the line:
plink.exe -ssh userID#Server
After the console window pops up, I repeat the steps 2 to 4 and script runs successfully.
What I am failing to do so is to automate the whole process.
Here's the line in my .cmd file which I attempted:
plink.exe -ssh userID#Server /myfilepath/myscript.sh %arg1% %arg2%
I can see the arguments passed correctly using multiple echo in the shell script. However, the shell script fails to locate the .sql file.
Error log:
/mypath/myscript.sh[1]: !/bin/sh^M not found [No such file or directory]
myarg1value
myarg2value
:No such file or directory[myarg1value]
/mypath/myscript.sh[12]: sqlplus: not found [No such file or directory]
I also tried below, but unfortunately with same result:
plink.exe -ssh userID#Server -m command.txt
Where file command.txt contains:
. my_env
cd /filepath/
./myscript.sh %arg_with_actual_value%
I do not know why it is not working, especially when 2) works and the script is relatively simple.
Do I assume things incorrectly about plink (path, variable, etc.)?
Is Cygwin the only way out?
I tried not to rely on yet another tool as I have been using plink.
EDIT: While the line
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
fails to run on the .sh, i can run it on the .cmd file itself via:
plink.exe -ssh userID#Server sed -i "s/%arg3%/%arg4%/g" /myfilepath/sequence.%arg1%.sql
Hence I am suspecting the problem comes from the .sh file not having the required components to run (i.e. set env variable, path, etc)
This is not a solution but partially fixed some issue, thanks to Martin Prikryl and Mofi's input:
in the command.txt, the following needs to be set:
ORACLE_SID
ORACLE_HOME
PATH
after these are set, the sqlplus and sed will work normally. However, passing values from .cmd through plink to Linux's shell script seems to have issue with the actual value being passed. The variable will have the assigned value along with some unreadable characters. In this case,
sqlplus $arg2/$arg2#MYDB
login fails because arg2 contains some other char.
#sequence.$arg1.sql
this line also fails as it will try to opens 2 files, one being called sequence.myvalue and another one called "%s", which i suspect the assigned variable contains some sort of unreadable nextline character.
EDIT: fixed, we can use the same treatment from sed - run sqlplus directly from plink instead of passing value and running a .sh script in Linux:
sqlplus $arg2/$arg2#MYDB #/myfilepath/sequence.%arg1%.sql
Here is my assignment for class: I know you don't post specific questions, but here's what I have tried and it isn't working. I was hoping for someone to point me in the direction and I can go from there:
Write a shell script that performs the following functions:
as the first command in your shell script use the script -a ch10-q1.txt command.
use the echo command to display the hostname, logname, and home system variables.
displays the current date and time using Coordinated Universal Time.
displays the list of directories in the user's home directory and all the subdirectories below (hint: use the tree command with the appropriate option).
list all the files in the user's home directory and all the subdirectories below (hint: check the options). Also use the -gF options.
use the df command to display the space usage in your system. Use the option(s) to include the total size in human readable format.
Save your shell script file in your home directory and name it ch10-1
Here is my code in vi ch10-1.
# !/bin/bash
script -a ch10-q1.txt
echo $hostname
echo $date -u
echo $ls -d */u
echo $ls -la
echo $ls gf
echo $df; df -h
Then I save the file and make the file executable by:
chmod 777 ch10-1
I try and run the program by:
./ch10-1
And then it tells me that line 2-8 command not found.
I guess my questions is how do I have multiple commands?
$hostname
The command to display the hostname.
I want to run a bash script on startup of my Parallella board, which has Ubuntu. I searched in SO, and found the instruction in here:
Run automatically program on startup under linux ubuntu
Programmatically run at startup on Linux?
My bash script is test.sh, which has only one line:
echo "Hello World" &> /home/qsp/WIP/test/hello.txt
1) The first way I tried is adding to /etc/rc.local the aboslute path to the script:
/home/qsp/WIP/test/test.sh
2) The second way I tried is following the accepted answer above.
sudo mv test.sh /etc/init.d/
sudo update-rc.d test.sh defaults
In both cases, the script was executed after booting, and there was a file hello.txt created in the folder. However, the content of the file is empty (and the owner is root). I wonder if I'm missing anything. Thank you.
======UPDATE=======
Following the answer of Skynet, I change my script to:
echo "Hello World" | tee /home/qsp/WIP/test/hello.txt
and the script writes to the file after booting correctly. I have another question, why my original script with &> didn't work, although it still works if running from command line.
You should make it in init script style, as cited by the first SO question. Like so:
case "$1" in
start)
#startup code
;;
stop)
#stop code
;;
restart)
#restart code
;;
esac
Also take a look at https://github.com/fhd/init-script-template/blob/master/template
After editing /etc/rc.local and adding your commands,
check your script must always end with exit 0.
Also make it sure you made it executable by using chmod command
chmod 777 test.sh
Change the line of output as
echo "Hello World" | tee /home/qsp/WIP/test/hello.txt
Create .desktop file and configure your ystem to auto-start at the time of login
Create .desktop file as below
$ vim ~/.config/autostart/test_script.desktop
add the below information
[Desktop Entry]
Type=Application
Name=Test script
Exec=~/test.sh
X-GNOME-Autostart-enabled=true
Note that ~/test.sh should point to the script you've created. Save it.
Make it executable:
$ chmod o+x ~/.config/autostart/test_script.desktop
Reboot and for the next login your script should run.