30
votes
Accepted
Run a script from anywhere
You want to type abspath, but the program is named abspath.sh. The problem is not regarding whether it is in the PATH, but the fact that you are simply not using its name to call it.
You have two ...
24
votes
Accepted
How to compress this mysqldump?
I think you can just do this.
/usr/bin/mysqldump -h $mysql_host -u $mysql_username -p$mysql_password $mysql_database | gzip -9 -c > $backup_path/$today/$mysql_database-`date +%H%M`.sql.gz
21
votes
How do you set a locale non-interactively on Debian/Ubuntu?
Based on the fine work in @EirikW's answer. Specific to a Dockerfile:
ENV LANG=en_GB.UTF-8
RUN apt-get install -y locales && \
sed -i -e "s/# $LANG.*/$LANG UTF-8/" /etc/locale....
11
votes
Accepted
Automated graceful reload of gunicorn in production
Add the following to the systemd service file for gunicorn, or add it as an override:
ExecReload=/bin/kill -HUP $MAINPID
You can then reload with systemctl reload gunicorn.
11
votes
Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?
I'm unsure if you want to actually query nameservers or rather rely on your system's name resolution:
$ getent ahosts one.one.one.one. | awk '{ print $1 }' | sort -u
1.0.0.1
1.1.1.1
2606:4700:4700::...
10
votes
What does a minus sign inside of dollar brackets of a shell script mean?
It should be emphasized that this means an unset variable. Not an empty one.
And to put to comparison with :-, which will use default value (the one after minus sign) if variable is unset or null (as ...
10
votes
Get list of transferred files from rsync?
Use the --out-format option
According to the man page:
Specifying the --out-format option will mention each file, dir, etc.
that gets updated in a significant way (a transferred file, a
...
10
votes
Accepted
FreeBSD rc.d script doesn't start as a daemon
The RC script itself is not intended to daemonize, but is expected to start and stop the daemon.
If your service does not have an option to start as a daemon, you can use daemon(8) to manage that ...
9
votes
Echo each shell script command with a timestamp
In Bash, you can use PS4 - which is used by set -x to prefix tracing output.
PS4='+ $(date "+%s.%N")\011 '
set -x
...
\011 is the horizontal tab character
9
votes
Accepted
shell script to delete 6 month old logs from log file
The solution really is to use logrotate: it works very well, and it can also compress the rotated log.
If you really don't want to use logrotate, you had to manually cat/copy the relevant section of ...
8
votes
How do you set a locale non-interactively on Debian/Ubuntu?
For me it was necessary to set the additional 3 ENV-Vars:
# - Set the locale + timezone --------------------------------------------------
RUN echo "Europe/Vienna" > /etc/timezone && \
...
8
votes
Loop through servers and run command
There is solution with -n -x parameters:
http_status=$(ssh -nx name ps -ef | grep -v grep | grep $service | wc -l)
The -x disables X11 forwarding to get rid of possible X11 forwarding request failed ...
8
votes
Accepted
Closing All Shared Files on the Network
The command you are using is not a native PowerShell command. However, PowerShell does come with a lot of functions to control SMB connections and shares.
What you are looking for could be this:
Get-...
7
votes
Using curl with cookies
It is possible that their website is authenticating the session stored in the cookies by redirecting them first to the login page. Then their login page will redirect back to the appropriate page as ...
7
votes
Accepted
Echo each shell script command with a timestamp
in Bash, execution of code after each command can be achieved using the "trap" builtin and the "DEBUG" level.
in bash do: help trap
or refer to bash manual page, and look for the trap section
...
7
votes
How to compress this mysqldump?
Just 'pipe the output' to gzip
The command would become this:
/usr/bin/mysqldump -h $mysql_host -u $mysql_username -p$mysql_password $mysql_database | gzip > $backup_path/$today/$mysql_database-`...
7
votes
Accepted
Loop through servers and run command
You need to redirect the stdin of SSH command in your loop to nowhere to prevent reading all your lines. This should do it:
http_status=$(ssh $name "ps -ef | grep -v grep | grep $service | wc -l" <...
7
votes
Accepted
Can't access instance after setting up iptables rules
For rules in the OUTPUT chain you should specify the source port match (--sport), not the destination port (--dport).
Anyway DROP policy in the OUTPUT chain isn't a common practice.
Read the iptables ...
7
votes
Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?
You are on the right track with dig. You can suppress that particular cookie related error (per the man page). Below, I am also choosing to redirect all errors to /dev/null just in case, as you wanted ...
7
votes
Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?
getent ahosts[v4|v6] and generally anything that uses getaddrinfo() is the right answer, as it will use the same name resolution mechanisms (DNS, mDNS, NetBIOS, NIS, LDAP…) as the rest of the system, ...
6
votes
Run a script from anywhere
This code is small enough that I would code it as a shell function:
abspath() {
echo "$(dirname "$(readlink -e "$1")")/$(basename "$1")"
}
And yes you do want all those quotes.
6
votes
Installing and configuring phpmyadmin completely through a shell script
Basically the first thing to do is skipping any interactive post-install configuration steps.
export DEBIAN_FRONTEND=noninteractive
apt-get -yq install phpmyadmin
This will skip all the questions ...
6
votes
Accepted
How to start a jar file using systemd
Using systemd directives in place of shell results in a more consistent environment. This includes not relying on $PATH or the working directory by providing full paths where they are known.
Neither &...
6
votes
Trying to execute this shell script but getting this error
Your script has DOS line endings. Convert them to Linux line endings either in your editor or with tools like dos2unix, recode etc.
DOS/Windows usually ends lines with CR+LF, while Linux only uses ...
6
votes
Accepted
Running a binary as a service in RedHat/CentOS
Have you looked into the below error message?
Apr 16 06:29:32 matrix-pipeline-b-01 executePipeline.sh[10195]: sudo: no tty present and no askpass program specified
Apr 16 06:29:32 matrix-pipeline-b-...
6
votes
Accepted
Using blkid to check if an attached EBS volume is formatted
You can check the details of a specific device and act based on that:
blkid --match-token TYPE=ext4 /dev/nvme123 || mkfs.ext4 -m0 /dev/nvme123
What this does is:
blkid checks whether /dev/nvme123 ...
6
votes
Accepted
Is it possible to listen to a TCP port only with a shell, with no additional tools?
No, a basic Bourne/POSIX shell (/bin/sh) cannot be expected to include any built-in facilities for TCP connections. See comparision of command shells in Wikipedia.
The bash shell would have TCP and ...
5
votes
How do I sleep for a millisecond in bash or ksh
I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
#include "stdio.h"
int main(int argc, char **argv) {
if(argc == 2) { usleep(atoi(argv[1])); }
return 0;
}
...
5
votes
How to compress this mysqldump?
You can pipe it though gzip (or bzip2, pbzip2, xz, etc...) like so:
/usr/bin/mysqldump -h $mysql_host -u $mysql_username -p$mysql_password $mysql_database | gzip -c > $backup_path/$today/$...
5
votes
Accepted
Select remote database from bash
you need a subshell. try this:
count=$(mysql -u mysql -pMysql123 -h xxx.xxx.xxx.xxx MYDBNAME -sse "select count(column) from TABLE where column=1234;")
if [ $count -gt 0 ]
then
echo " greater ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shell-scripting × 622bash × 257
linux × 233
shell × 113
scripting × 67
ubuntu × 37
cron × 37
unix × 36
ssh × 35
mysql × 23
solaris × 22
powershell × 20
ksh × 19
awk × 18
command-line-interface × 17
unix-shell × 16
redhat × 13
windows × 12
centos × 12
perl × 12
sed × 12
expect × 12
debian × 10
freebsd × 10
backup × 9