Tag Info

Hot answers tagged

20

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 ...


17

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 $#


17

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 ...


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 ...


16

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.


12

command1 & command2 Will execute command1 in 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 command2 once command1 ...


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 ...


11

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 *. ...


11

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. ...


10

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


9

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 ...


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

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

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

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


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

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

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

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 ...


7

Use the modulo operator (bash specific syntax): if [ $(($counter % 600)) -eq 0 ] ; then echo " still in process wait another 10 min .." fi Or more portable: if [ `expr $counter % 600` -eq 0 ] ; then echo " still in process wait another 10 min .." fi


7

On the CentOS box I have to hand dig +short www.google.com 74.125.132.147 74.125.132.99 74.125.132.103 74.125.132.104 74.125.132.105 74.125.132.106 If you only want one address then dig +short www.google.com | head -1 or dig +short www.google.com | tail -1


7

If you are using bash, you should use the wait command, rather than an elaborate polling loop. I don't know about other shells (sh, zsh, and so on), but I assume most/all of them also have wait, or an equivalent to it.


7

The code samples on that site are erroneous. They have certain shell characters replaced with HTML entities. The quotes also seem to be in the wrong places. I wonder if the author ever looked at that page when writing it... Once we fix that, and then check to see if the first of the month is in two days rather than one... 0 23 27-30 * * [ $(date +\%d -d "2 ...


6

Yet another answer... mysql -uroot -pmysql -B --skip-column-names -e'SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb" FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema="database_name"' Also, if you are going to have the password in the script, ...


6

GNU tar has a -T option to take the list of files from a specified file. I would use find ... -print0 | tar cfzT outfile.tgz - --null so tar receives null-terminated filenames on stdin.


6

Alright, I should have done this to start with: FTPHOST="domain.com" FTPUSER="xxxxxx" FTPPASS="xxxxxxxxx" MEDIAFILE=/path/to/something.enc if [ -r $MEDIAFILE ] # File seems to exist and is readable then ftp -n $FTPHOST <<END_SCRIPT quote USER $FTPUSER quote PASS $FTPPASS cd backups bin put $MEDIAFILE something.enc quit END_SCRIPT fi I added the ...


6

inotify suggested by SvenW is an excellent choice for this. Just to be different: Alternative, more complete way for this kind of stuff is to use kernel audit subsystem and auditd. Just install auditd if it's not already installed and then use auditctl, in your case auditctl -w /usr/local/mydir/ -p wa -F uid=john -k johnschangedfiles would do. Then you ...



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