Cron job - exported file emailed is blank - shell

I am receiving the email from the following CRON job but with a blank file attached. I have checked the /home/XXXXXX/public_html/tmp/ directory and a good tqhsa_hikashop_order.txt file is created in that /tmp/ folder but the one attached to the email that I receive is empty.
mysql --database=XXXXXXXX_jml_6UCHadruwR8veju3 -u XXXXXXXX_8eh6Nu --password=fRa6r7wRerEtuzud -B -e "SELECT order_number, order_created, order_invoice_number, order_full_price, order_discount_code, order_discount_price, order_payment_method, order_payment_price FROM tqhsa_hikashop_order;" > /home/XXXXXXXX/public_html/tmp/tqhsa_hikashop_order.txt | mail -s "Daily Discount Table Dump" -a /home/XXXXXXXX/public_html/tmp/tqhsa_hikashop_order.txt luke#example.com
Any ideas why the good text file is not being attached?

You need to ensure that your first command finishes before running the second, so that the file exists on disk.
So essentially replace the | with a &&:
mysql \
--database=XXXXXXXX_jml_6UCHadruwR8veju3 \
-u XXXXXXXX_8eh6Nu \
--password=fRa6r7wRerEtuzud \
-B \
-e "SELECT order_number, order_created, order_invoice_number, order_full_price, order_discount_code, order_discount_price, order_payment_method, order_payment_price FROM tqhsa_hikashop_order;" > /home/XXXXXXXX/public_html/tmp/tqhsa_hikashop_order.txt \
&& \
mail \
-s "Daily Discount Table Dump" \
-a /home/XXXXXXXX/public_html/tmp/tqhsa_hikashop_order.txt luke#example.com
To illustrate using an example:
myuser#myshell:~ $ echo "testing" > testy.txt | cat testy.txt
cat: testy.txt: No such file or directory
myuser#myshell:~ $ echo "testing" > testy.txt && cat testy.txt
testing

Related

Set csh prompt to raw output of a command with multiple lines and special characters?

I am trying to set my csh prompt to the raw output of a particular command. I know how to do that for simple outputs; for example:
set prompt=`echo propmt%|sed "s/pm/mp/"`
... results in a prompt of:
prompt%
But the prompt I want is more complex. First, it has multiple lines. I know how to do that directly (i.e. not via the output of a command):
set prompt="This is a line\nThis is another\nAnd this is the last% "
... results in:
This is a line
This is another
And this is the last%
But if I try to do that same thing through a command:
set prompt=`echo "This is a line\
This is another\
And this is the last% "`
... the prompt becomes just:
This
... not even This is a line; just This.
On top of that, the specific command that I want to use will output not just multiple lines, but also a bunch of other characters that I assume csh and/or set will interpret in ways that I don't intend. Specifically:
cowsay -f ghostbusters "BOOOOOOOOOOOOOOOOOOOOOOOOO \
? \
? You are using CEEEEEE SHELLLLLLLLLLLLLLLLL\
? \
? Be my guest, but you have no one to blame but\
? YOURSELLLLLLLLLLLLLLLLLLLLLLF"; echo; echo "% "
... which outputs:
_________________________________________
/ BOOOOOOOOOOOOOOOOOOOOOOOOO \
| |
| You are using CEEEEEE |
| SHELLLLLLLLLLLLLLLLL |
| |
| Be my guest, but you have no one to |
\ blame but YOURSELLLLLLLLLLLLLLLLLLLLLLF /
-----------------------------------------
\
\
\ __---__
_- /--______
__--( / \ )XXXXXXXXXXX\v.
.-XXX( O O )XXXXXXXXXXXXXXX-
/XXX( U ) XXXXXXX\
/XXXXX( )--_ XXXXXXXXXXX\
/XXXXX/ ( O ) XXXXXX \XXXXX\
XXXXX/ / XXXXXX \__ \XXXXX
XXXXXX__/ XXXXXX \__---->
---___ XXX__/ XXXXXX \__ /
\- --__/ ___/\ XXXXXX / ___--/=
\-\ ___/ XXXXXX '--- XXXXXX
\-\/XXX\ XXXXXX /XXXXX
\XXXXXXXXX \ /XXXXX/
\XXXXXX > _/XXXXX/
\XXXXX--__/ __-- XXXX/
-XXXXXXXX--------------- XXXXXX-
\XXXXXXXXXXXXXXXXXXXXXXXXXX/
""VXXXXXXXXXXXXXXXXXXV""
%
That's what I want as my csh prompt. At this point, I feel obligated to note that I am not kidding.
I suppose I could forget about running a command to generate it, and instead just manually converting it to a single-line string that's properly escaped for set prompt, but:
That seems like a pain;
I'd like to be able to quickly and easily change the prompt to similar but different things.
So, I'm hoping for some way to tell set prompt "Just use the raw output of (arbitrary-command); don't interpret any of it as special characters or whatever". Is there a way? Or any alternate suggestions to accomplish what I want? Thanks.
Maybe it's better to use the precmd alias for this:
alias precmd 'cowsay -f ghostbusters << EOF \
line 1 \
\
line 2 \
\
line 3 \
EOF'
From csh(1):
precmd Runs just before each prompt is printed. For example, if one
does
> alias precmd date
then date(1) runs just before the shell prompts for each command.
There are no limits on what precmd can be set to do, but
discretion should be used.
Or you can make it a different file to avoid all the backslashes:
alias precmd 'source ~/cowsay'
# ~/cowsay
cowsay -f ghostbusters << EOF
line 1
line 2
line 3
EOF

Saving a single log file from multiples sh scripts

I have 2 sh scripts that saves log files accordingly, but and I want to create another sh to call them.
They are using a property file that contains 3 log functions
my first sh(bteq.sh) creates a table in my Teradata
#!/bin/bash
source "/valdc_procs/properties/general_config_file"
exec_logs "/valdc_procs/logs/" "BTEQ_VALDC_PROCS"
echo "INFO : Starting table creation"
function exec_btqe (){
bteq<<EOF 2>&1
.logmech LDAP
.LOGON ${ipServerPR}/${userNamePR},${passwordPR};
.SET TITLEDASHES OFF;
.SET WIDTH 2000
.SET SEPARATOR ';'
SELECT 1
FROM DBC.TABLES
WHERE DatabaseName = ''
AND TableName = ''
AND TableKind = 'T';
.IF ACTIVITYCOUNT = 0 THEN .GOTO CreateNewTable;
DROP TABLE ;
.LABEL CreateNewTable;
CREATE MULTISET TABLE xpto AS (
SELECT
...
)
WITH DATA;
.LOGOFF;
.QUIT;
EOF
}
exec_btqe
#it gets the rc of the last command if it's an error
rc_result "Error creating table " "/valdc_procs/logs/" "BTEQ_VALDC_PROCS"
#it logs the process when there were no errors
log_output "/valdc_procs/logs/" "BTEQ_VALDC_PROCS"
exit $rc
my second sh(tdch.sh) export that table to a file
#!/bin/bash
source "/valdc_procs/properties/general_config_file"
exec_logs "/valdc_procs/logs/" "TDCH_VALDC_PROCS"
hadoop jar $TDCH_JAR com.teradata.connector.common.tool.ConnectorImportTool \
-libjars $LIB_JARS \
-Dmapred.job.queue.name=default \
-Dtez.queue.name=default \
-Dmapred.job.name=TDCH \
-classname com.teradata.jdbc.TeraDriver \
-url jdbc:teradata://$ipServer/logmech=ldap,database=$database,charset=UTF16 \
-jobtype hdfs \
-fileformat textfile \
-separator ',' \
-enclosedby '"' \
-targettable ${targetTable} \
-username ${userName} \
-password ${password} \
-sourcequery "select * from ${database}.${targetTable}" \
-nummappers 1 \
-sourcefieldnames "" \
-targetpaths ${targetPaths}
#it gets the rc of the last command if it's an error
rc_result "Error exporting the file " "/valdc_procs/logs/" "TDCH_VALDC_PROCS"
echo "INFO : Moving file from HDFS to the FileSystem"
hdfs dfs -copyToLocal ${targetPaths}/"part-m-00000" ${targetFileSystemPath}/ARQ_VALDC_PROCS_OPBK_$TIMESTAMP
rc_result "Error moving the file " "/valdc_procs/logs/" "TDCH_VALDC_PROCS"
echo "INFO : File Moved Arquivo movido com sucesso"
#it logs the process when there were no errors
log_output "/valdc_procs/logs/" "TDCH_VALDC_PROCS"
exit $rc
This log process is working fine when I run each sh individually, but now I want to have another sh calling them.
But I'm not sure how can I get the rc of their commands(bteq.sh and tdch.sh) and save in a single log file
Not 100% certain I understand your need, but that might solve your question:
#!/bin/bash
logfile="/tmp/somefile.txt"
bteq.sh
status_bteq=$?
tdch.sh
status_tdch=$?
echo "Status bteq: $status_bteq" >"$logfile"
echo "Status tdch: $status_tdch" >>"$logfile"
If you want just the return codes, raw without anyother text, you could do:
#!/bin/bash
logfile="/tmp/somefile.txt"
bteq.sh
echo "$?" >"$logfile"
tdch.sh
echo "$?" >>"$logfile"

How to run a script with correct permissions from custom udev rule?

I set up a custom udev rule triggered when my bluetooth mouse connects:
ACTION=="add" \
, ATTRS{idProduct}=="XXX" \
, ATTRS{idVendor}=="XXX" \
, ENV{DISPLAY}=":0" \
, ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority" \
, RUN+="/home/XXX/.scripts/mouse_connected.sh"
udevadm test succeeds, everything seems fine.
Then i created my script that should be triggered:
#!/bin/bash
if (xinput list | grep Maus | grep -o id=[0-9]* | grep -o [0-9]*$);
then xinput set-button-map "$(xinput list | grep Maus | grep -o id=[0-9]* | grep -o [0-9]*$)" 1 1 3 && logger "success";
else logger "fail" && sleep 3;
fi
when I manually run the script in terminal using "bash '/home/XXX/.scripts/mouse_connected.sh'" it works as expected. But when it gets successfully triggered via my custom udev rule it fails. - But why?

Issue on concatenating files shellscipt

Sorry, I'm from Brazil and my english is not fluent.
I wanna concatenate 20 files using a shellscript through cat command. However when I run it from a file, all content of files are showed on the screen.
When I run it directly from terminal, works perfectly.
That's my code above:
#!/usr/bin/ksh
set -x -a
. /PROD/INCLUDE/include.prod
DATE=`date +'%Y%m%d%H%M%S'`
FINAL_NAME=$1
# check if all paremeters are passed
if [ -z $FINAL_NAME ]; then
echo "Please pass the final name as parameter"
exit 1
fi
# concatenate files
cat $DIRFILE/AI6LM760_AI6_CF2_SLOTP01* $DIRFILE/AI6LM761_AI6_CF2_SLOTP02* $DIRFILE/AI6LM763_AI6_CF2_SLOTP04* \
$DIRFILE/AI6LM764_AI6_CF2_SLOTP05* $DIRFILE/AI6LM765_AI6_CF2_SLOTP06* $DIRFILE/AI6LM766_AI6_CF2_SLOTP07* \
$DIRFILE/AI6LM767_AI6_CF2_SLOTP08* $DIRFILE/AI6LM768_AI6_CF2_SLOTP09* $DIRFILE/AI6LM769_AI6_CF2_SLOTP10* \
$DIRFILE/AI6LM770_AI6_CF2_SLOTP11* $DIRFILE/AI6LM771_AI6_CF2_SLOTP12* $DIRFILE/AI6LM772_AI6_CF2_SLOTP13* \
$DIRFILE/AI6LM773_AI6_CF2_SLOTP14* $DIRFILE/AI6LM774_AI6_CF2_SLOTP15* $DIRFILE/AI6LM775_AI6_CF2_SLOTP16* \
$DIRFILE/AI6LM776_AI6_CF2_SLOTP17* $DIRFILE/AI6LM777_AI6_CF2_SLOTP18* $DIRFILE/AI6LM778_AI6_CF2_SLOTP19* \
$DIRFILE/AI6LM779_AI6_CF2_SLOTP20* > CF2_FINAL_TEMP
mv $DIRFILE/CF2_FINAL_TEMP $DIRFILE/$FINAL_NAME
I solved the problem putting the cat block inside a function, and redirecting stdout to the final file.
Ex:
concatenate()

How to send message from zabbix to telegram?

I have problems with notifications on zabbix to telegram messenger.
So, I specified different guides for that. But not successful.
For example I use this guides
This solutions works for bash. But I can send this from zabbix.
export to=$1;
export subject=$2;
export body=$3;
tgpath=/usr/src/tg/zabbix
cd ${tgpath}
(sleep 5; echo "msg $to $subject $body"; echo "safe_quit") |
${tgpath}/telegram-cli -k /etc/telegram-cli/mykey.pub -W
Key telegram-cli -e does not work correctly with a login name and with format user#XXXXXX;
I dont want to use some API to send message.
Thank you for any help.
Your script is not equal to the blog post.
The steps are:
0 - Compile
cd /usr/src
git clone --recursive https://github.com/vysheng/tg.git
cd tg
./configure
make
mkdir viacron
cp bin/telegram-cli viacron/
cp tg-server.pub viacron/
cd viacron
1 - Create a file /usr/src/tg/viacron/telegram.config an put this:
default_profile = "viacron";
viacron = {
config_directory = "/usr/src/tg/viacron/";
};
2 - Create a file /usr/src/tg/viacron/telegram.config an put this:
#!/bin/bash
MAIN_DIRECTORY="/usr/src/tg/viacron/"
USER=$1
SUBJECT=$2
TEXT=$3
cd $MAIN_DIRECTORY
if [[ $? -ne 0 ]]; then
echo "Error to enter in the main directory"
exit 1
fi
./telegram-cli -k tg-server.pub -c telegram.config -WR -e "msg $USER $SUBJECT" || exit 1
exit 0
3 - Change permissions:
chmod +x /usr/src/tg/viacron/telegram_standalone.sh
chown -R yourUser: /usr/src/tg/
4 - Test:
/usr/src/tg/viacron/telegram_standalone.sh user#12345 "GNU is not unix"
5 - Put AlertScriptsPath=/usr/src/tg/viacron/ in zabbix_server.conf and restart the server
6 - In zabbix, add new media type with name telegram_standalone.sh
More information in https://gist.github.com/gnumoksha/a95f237d82733ce1f748 and http://tobias.ws/blog/zabbix-com-notificacoes-pelo-telegram/
Now Telegram supported by default
check:
https://www.zabbix.com/integrations/telegram
and there is extra settings check:
https://youtu.be/TpP6NpS9jjg?t=143

Resources