Tag Info

Hot answers tagged

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


13

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


11

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

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. command1 && command2 will only execute command2 once command1 has completed execution successfully. If command1 ...


10

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


10

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


10

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


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.


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;


8

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


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


7

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


7

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


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

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.


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.


6

CURRPATH is not the directory the script is in, it's the working directory that the script inherited from its parent process; for cron jobs, this is the home directory of the account it's running under (i.e. ~root). If you want the directory the script is in, use CURRPATH=$(dirname "$BASH_SOURCE") instead.


6

I believe something as simple as 'sort -u ' should work for you #sort -u /tmp/test 172.17.200.1 3.3.3.3 172.17.200.2 3.3.3.4 172.17.200.3 3.3.3.5 172.17.200.4 3.3.3.7 172.17.200.5 3.3.3.8 255.255.255.0 255.255.255.111 Check the 'sort' manpage for more info: -u, --unique with -c, check for strict ordering; without -c, output only the first of an equal ...


6

You don't need the while loop and you should use rmdir -p to remove the empty parents find . -depth -type d -empty -exec rmdir -v -p {} + rmdir: removing directory, `./1/3' rmdir: removing directory, `./1' rmdir: removing directory, `.' rmdir: failed to remove directory `.': Invalid argument rmdir: removing directory, `./2/3' rmdir: removing directory, ...


6

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.


6

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


6

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


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

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


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

For Debian: sudo -u www-data test -r /path/to/file && echo ok


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