Hot answers tagged

46

The documentation for the sleep command from coreutils says: Historical implementations of sleep have required that number be an integer, and only accepted a single argument without a suffix. However, GNU sleep accepts arbitrary floating point numbers. See Floating point. Hence you can use sleep 0.1, sleep 1.0e-1 and similar arguments.


30

Bash has a "loadable" sleep which supports fractional seconds: $ cd bash-3.2.48/examples/loadables $ make sleep && mv sleep sleep.so $ enable -f sleep.so sleep Then: $ which sleep /usr/bin/sleep $ builtin sleep sleep: usage: sleep seconds[.fraction] $ time (for f in `seq 1 10`; do sleep 0.1; done) real 0m1.000s user 0m0.004s sys ...


22

You do this by creating a subroutine you want to call when SIGINT is received, and you need to run trap 'subroutinename' INT. Example: #!/bin/bash int_handler() { echo "Interrupted." # Kill the parent process of the script. kill $PPID exit 1 } trap 'int_handler' INT while true; do sleep 1 echo "I'm still alive!" done # We never ...


18

Cheap n' dirty way: Loop stat -c %Y file and take action when the modification time changes. Probably better: Use the inotify cron service to watch for file modification events and run your action: /path/to/your/file IN_MODIFY /path/to/your/script $#


16

Edit: I ran this with your source file in my environment and have the following results: [root@xt ~]# time tiff2ps test.tif > test.ps real 0m0.795s user 0m0.659s sys 0m0.135s [root@xt ~]# time ps2pdf13 -sPAPERSIZE=a4 test.ps > test.pdf real 0m0.592s user 0m0.513s sys 0m0.075s [root@xt ~]# time tiff2ps test.tif | ps2pdf13 ...


15

Sleep accepts decimal numbers so you can break it down this like: 1/2 of a second sleep 0.5 1/100 of a second sleep 0.01 So for a millisecond you would want sleep 0.001


14

I think you are almost there. put a / at the end of your directory spec jsmith ALL=(ALL) NOPASSWD: /usr/local/tomcat7/bin/ From the sudoers man page A directory is a fully qualified path name ending in a '/'. When you specify a directory in a Cmnd_List, the user will be able to run any file within that directory (but not in any subdirectories ...


14

Both for MySQL and PostgreSQL you can specify your user and password in local config file. .my.cnf for MySQL and .pgpass for PostgreSQL. These files should be in your home directory (i.e. ~/.my.cnf). .my.cnf: [mysql] user=user password=password .pgpass: host:port:database:user:password You can have a wildcard entry here, substituting any field for ...


13

command1 & command2 Will execute command1, send the process to the background, and immediately begin executing command2, even if command1 has not completed. command1 ; command2 Will execute command1 and then execute command2 once command1 finishes, regardless of whether command1 exited successfully. command1 && command2 will only execute ...


12

Well the ";" makes the shell wait for the command to finish and then continues with the next command. The "&" will send any process directly into the background and continues with the next command - no matter if the first command finished or is still running. So "&;" will not work like you expect. But actually I'm unsure what you expect. Try ...


12

If the teamcity start script is a LSB-compliant init script (that is, if it provides start, stop and other arguments), you can just copy the script to /etc/init.d folder and run (as root): # chkconfig --add <script_name> # chkconfig <script_name> on A symbolic link for the file should be OK too. So try (as root): # ln -s ...


12

On a default UNIX-like system, cron has a minimal environment defined. Usually HOME, SHELL, LOGNAME are defined and PATH is set to /bin. You have two solutions: enter the full path of your script. ie: /home/me/bin/ec2-automate-backup.sh alter the PATH environment variable. If you want to receive email from the cron daemon define the MAILTO variable. ...


12

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 options: Type abspath.sh instead. Rename the program to abspath.


10

Your condition isn't actually calling check_space, you need something like: if [ `check_space` -gt "85" ]; then


10

if [ $(mysql -N -s -u root -p -e \ "select count(*) from information_schema.tables where \ table_schema='db_name' and table_name='table_name';") -eq 1 ]; then do something else echo "table <table_name> does not exist" exit 1 fi -N to skip column names -s for nontabular output


10

Instead of using the psql \password command, which expects an interactive terminal, you can use: ALTER USER postgres WITH PASSWORD 'newpassword'; Say, via a psql -c command: sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'newpassword';"


10

The $? is evaluated before the command is sent to the remote machine. Effectively, you are sending the command echo 0. Try ssh server ' <some command >; echo $? ' As Michael Hampton points out, single quotes prevent evaluating variables etc. before sending them to the remote command, while double quotes allow that.


9

Most interpreters have a parameter to specify some code to execute. You can use this to invoke a specific interpreter and provide the code. For example: bash -c 'function run_free() { free -m; }; run_free' (Note that you need some semicolons in there since you don't have newlines.) perl uses -e, and python uses -c.


9

Specifying correct type for password should be enough, try: Param ( [Parameter(Mandatory=$True)] [string]$FileLocation, [Parameter(Mandatory=$True)] [Security.SecureString]$password ) PowerShell will "mask" password (same as for read-host -asSecureString) and result type will be the one that other cmdlets may require. EDIT: After recent ...


9

Yes, you can mount a windows share into the linux filesystem using mount.cifs e.g. mkdir /mnt/windowsfs mount -t cifs //server/share -o username=user,password=P4ssw0rd /mnt/windowsfs Once you've done that you can (with suitable permissions) access and manipulate the files in /mnt/windowsfs.


9

You disable it by adding the following line in your script: set -o noglob As an example, echo * your files and folders are shown here.. set -o noglob echo * *


8

Instead of constantly pulling a directory this way, maybe try to use the inotify subsystem, which is designed for this purpose. For user space tools, have a look at the inotify-tools.


8

Could not get the answer above to work. Instead, I use this method (for Dockerfiles): # Configure timezone and locale echo "Europe/Oslo" > /etc/timezone && \ dpkg-reconfigure -f noninteractive tzdata && \ sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \ sed -i -e 's/# nb_NO.UTF-8 ...


8

Look at the autodie core module. This replaces calls like open and fork with functions that die on failure. To get it to work with system, you need to import :all or :system, since the default does not do so. use strict; #always! use warnings; #always! use autodie qw(:system); system('/bin/false'); #This will die print "This will never be printed\n"; ...


8

Use the find command with the -user option. Something like: find / -user john will eventually turn up all files owned by user "john". If you want to change their ownership (I would run the find without execution to make sure you have the list you want), then something like: find / -user john -exec chown harry {} \; will do it.


8

You can put it into "": $ ls file1 file2 file3 file4 file5 file6 file7 file8 file9 $ Q='select * from table;' $ echo $Q select file1 file2 file3 file4 file5 file6 file7 file8 file9 from table; $ echo "$Q" select * from table;


7

Try sed "6s/old/new/g" file.txt


7

Use a heredoc. cat > /etc/apache2/sites-available/"$1" << EOF <VirtualHost> ServerAdmin support@$1 ... EOF


7

mysqldump returns 0 for Success 1 for Warning 2 for Not Found It also prints an extended error message to stderr e.g. mysqldump: Got error: 1049: Unknown database 'dbname' when selecting the database You can inspect the returned value like so mysqldump -u DBuser -pDBpassword database >database.sql 2>database.err if [ "$?" -eq 0 ] then echo ...


7

Try adding -t -t to your SSH connection options. This will; force a pseudo-terminal to be allocated. Multiple -t options force tty allocation, even if ssh has no local tty.



Only top voted, non community-wiki answers of a minimum length are eligible