While converting the date to +%-m/%-d/%Y using date format in unix, I'm getting dates converted to timezones. HKT or JST timezones to EST, I want the date as is to be converted as mm/dd/yy without this timezone conversion
Example:
$ date -u '--d=Sun Jan 25 08:39:12 JST 2015' +%-m/%-d/%Y
1/24/2015
I also tried with -u -d options
$ date -u -d 'Sun Jan 25 08:39:12 JST 2015' +%-m/%-d/%Y
1/24/2015
Can someone please help me with the solution.
To change the output timezone, you could set TZ envvar:
$ TZ=EST date -d 'Sun Jan 25 08:39:12 JST 2015'
Sat Jan 24 18:39:12 EST 2015
$ TZ=JST date -d 'Sun Jan 25 08:39:12 JST 2015'
Sun Jan 25 08:39:12 JST 2015
To ignore the input timezone or to detect it automatically, a Python script could be used:
#!/usr/bin/env python
"""Change date/time format.
Usage: $ {prog} <input-date-time> <output-format>
Example: $ ./change-date-format 'Sun Jan 25 08:39:12 JST 2015' %-m/%-d/%Y
Output: 1/25/2015
"""
import sys
from dateutil.parser import parse # apt-get install python-dateutil
try:
date_string, date_format = sys.argv[1:]
except ValueError:
sys.exit(__doc__.format(prog=sys.argv[0])) # print usage info
else:
datetime_object = parse(date_string)
print(datetime_object.strftime(date_format))
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I have a file which has multiple record of netstat output, my sample file looks something like the one below. I want to search for PID for ex : 3453, part of the output I also want to see snapshot time, so that I can find whether PID was exist part of particular snapshot or not. Any thoughts?
zzz ***Sat Apr 11 03:00:26 UTC 2020
USER PID PPID PRI %CPU %MEM VSZ RSS WCHAN S STARTED TIME COMMAND
test 1234 3445 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
test1 3453 6741 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
zzz ***Sat Apr 11 03:01:26 UTC 2020
USER PID PPID PRI %CPU %MEM VSZ RSS WCHAN S STARTED TIME COMMAND
test 3453 3453 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
test1 7842 8712 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
Expected sample output for search 3453:
zzz ***Sat Apr 11 03:00:26 UTC 2020
test1 3453 6741 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
zz ***Sat Apr 11 03:01:26 UTC 2020
test 3453 3453 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
With GNU sed:
sed -n '/\*\*\*/,/^$/{ /\*\*\*/p; /3453/p }' file
Output:
zzz ***Sat Apr 11 03:00:26 UTC 2020
test1 3453 6741 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
zzz ***Sat Apr 11 03:01:26 UTC 2020
test 3453 3453 19 2.4 1.9 4070932 3539756 futex_wait_queue_ S Apr 04 04:00:17 test -quiet
See: man sed and The Stack Overflow Regular Expressions FAQ
Has anyone tried using the bitbake-env utility lately?
I do not see it installed:
[user#localhost build]$ which bitbake
~/YOCTO/oe_core_embedded/poky/bitbake/bin/bitbake
[user#localhost build]$ which bitbake-env
/usr/bin/which: no bitbake-env in (/home/user/YOCTO/oe_core_embedded/poky/scripts:/home/user/YOCTO/oe_core_embedded/poky/bitbake/bin:/usr/libexec/python2-sphinx:/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/user/.local/bin:/home/user/bin)
[user#localhost build]$
As well I do not see it in the bitbake command listing:
[user#localhost poky]$ cd bitbake/bin
[user#localhost bin]$ ls -al
total 100
drwxrwxr-x. 2 user user 4096 Oct 18 12:49 .
drwxrwxr-x. 6 user user 4096 Oct 18 12:49 ..
-rwxrwxr-x. 1 user user 2072 Nov 18 11:51 bitbake
-rwxrwxr-x. 1 user user 7228 Oct 18 12:49 bitbake-diffsigs
-rwxrwxr-x. 1 user user 2894 Oct 18 12:49 bitbake-dumpsig
-rwxrwxr-x. 1 user user 4069 Oct 18 12:49 bitbake-layers
-rwxrwxr-x. 1 user user 2109 Oct 18 12:49 bitbake-prserv
-rwxrwxr-x. 1 user user 2128 Oct 18 12:49 bitbake-selftest
-rwxrwxr-x. 1 user user 17866 Oct 18 12:49 bitbake-worker
-rwxrwxr-x. 1 user user 13951 Oct 18 12:49 bitdoc
-rwxrwxr-x. 1 user user 5813 Oct 18 12:49 git-make-shallow
-rwxrwxr-x. 1 user user 8805 Oct 18 12:49 toaster
-rwxrwxr-x. 1 user user 4177 Oct 18 12:49 toaster-eventreplay
[user#localhost bin]$
It still exists/is listed in https://elinux.org/Bitbake_Cheat_Sheet from July 2016.
bitbake-env is a third party utility, and not actually a part of the OpenEmbedded Core hence why you will not see it unless you specifically install it. It is actually an 'improved' version of the bitbake -e flag for bitbake.
bash-4.3$ bitbake --help
Usage: bitbake [options] [recipename/target recipe:do_task ...]
Executes the specified task (default is 'build') for a given set of target recipes (.bb files).
It is assumed there is a conf/bblayers.conf available in cwd or in BBPATH which
will provide the layer, BBFILES and other configuration information.
Options:
...
-e, --environment Show the global or per-recipe environment complete
with information about where variables were
set/changed.
I am testing the date utility, and have found some unexpected behaviour. My test is to set the date to a given day/month/year, add 24 hours to it, and confirm that it returns the following date.
In my tests below, this works for November 4, 2017, and November 6, 2017, but not for November 5, 2017.
This is clearly a bug, isn't it? How could there be such a fundamental flaw in a key GNU utility?
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date=`date -d "2017-11-04" +%s`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date_new=`expr $date + 86400`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ req_t=`date -d #$date_new "+%Y%m%d"`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ echo $req_t
20171105
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date=`date -d "2017-11-06" +%s`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date_new=`expr $date + 86400`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ req_t=`date -d #$date_new "+%Y%m%d"`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ echo $req_t
20171107
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date=`date -d "2017-11-05" +%s`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date_new=`expr $date + 86400`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ req_t=`date -d #$date_new "+%Y%m%d"`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ echo $req_t
20171105
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date --version
date (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.
It appears you are concerned with the apparent discrepancy suggested by the following (noting that 86400 seconds = 24 hours)
2017-11-04 00:00 + 86400 seconds = 2017-11-05 00:00
2017-11-05 00:00 + 86400 seconds = 2017-11-05 24:00 (see below!)
2017-11-06 00:00 + 86400 seconds = 2017-11-07 00:00
This is not a bug.
You don't mention what timezone you are in, but in most of North America, 2017-11-05 was the day on which Daylight Saving Time ended, so it was 25 hours long (90000 seconds).
I am looking for a way to schedule a set of commands on a unix server. The server time is in UTC. I essentially want to perform the below steps automatically every Wednesday at 4pm UK time:
Change the server date to the next time it is UK midnight (to avoid timezone change issues)
Restart the tomcat server
If tomcat is running, run a jar
Change the server date to the correct present UTC time
Restart the tomcat server
If tomcat is running, we are done
The below commands are what I currently run manually:
date -s "Thu Feb 09 00:01:00 UTC 2017" (represents the next day at 1 minute past midnight)
service tomcat restart
sudo -u tomcat java -jar Test.jar -type "Major" -status "Active"
date -s "<the current UTC time>"
service tomcat restart
I understand we can use cron to schedule the running of a script, but unsure how to do this. Any help is appreciated.
Create a file called called unix-server-date.sh and save it in /opt for example. The script will have this content:
#!/bin/bash
date -s "Thu Feb 09 00:01:00 UTC 2017" (represents the next day at 1 minute past midnight)
service tomcat restart
sudo -u tomcat java -jar Test.jar -type "Major" -status "Active"
date -s "<the current UTC time>"
service tomcat restart
Make the script executable:
chmod +x /opt/unix-server-date.sh
Then issue crontab -e to edit the crontab entries and add an entry like:
00 16 * * 3 /opt/unix-server-date.sh
Depending on your editor, after you have added the crontab entry please save the file and the crontab will be automatically installed.
That would be the basics!
If you run those commands from a specific user account you should add the cronjob to that user's crontab. Change crontab -e to crontab -e -u user
I was googling and found this link helpful https://serverfault.com/questions/538233/qmail-limit-number-of-emails-sent-by-user-or-domain-per-hour
My OS is Centos with Plesk 11.x + Qmail
I tried my best to follow given directives in the link e.g.
1) I installed spamdkye successfully
2) Created mkdir -p /home/vpopmail/bin/qmail-antispam
3) mkdir -p /etc/qmailadmin/qmail-spam/blacklist
4) crontab -e
*/5 * * * * /home/vpopmail/bin/qmail-antispam >> /var/log/maillog 2>&1
5) I saved script as qmail-antispam as well as moved as blacklist but on both positions it displays:
Code:
# tail -f /var/log/maillog | grep "qmail-antispam"
/bin/sh: /home/vpopmail/bin/qmail-antispam: is a directory
/bin/sh: /home/vpopmail/bin/qmail-antispam: is a directory
Tue Oct 20 12:55:01 CDT 2015 qmail-antispam : Revisando logs
Tue Oct 20 12:55:01 CDT 2015 qmail-antispam : Fin de revision
Tue Oct 20 13:00:01 CDT 2015 qmail-antispam : Revisando logs
Tue Oct 20 13:00:01 CDT 2015 qmail-antispam : Fin de revision
Questions:
Please advise where:
1) where I save this script either in /etc/qmailadmin/qmail-spam/blacklist or /home/vpopmail/bin/qmail-antispam and with which name?
2) MAX_CORREOS=3000 //is that to set maximum limit??
ID_SERVER="ID_SERVER" //May I change this to my server hostname??
CONTACTO=admin#gmail.com //Here I set admin email to send report. Is that right??
Please advise
Thanks in anticipation
vi /home/vpopmail/bin/qmail-antispam
copy & paste the script provided in Link
Edit script and change following:
MAX_CORREOS=100
ID_SERVER="my server hostname"
CONTACTO=admin#gmail.com
# chown root /home/vpopmail/bin/qmail-antispam
# chmod 755 /home/vpopmail/bin/qmail-antispam
and check its working fine:
# tail -f /var/log/maillog | grep "qmail-antispam"
Wed Oct 21 19:20:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:25:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:30:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:35:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:40:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:45:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:50:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 19:55:01 CDT 2015 qmail-antispam : Revisando logs
Wed Oct 21 20:00:01 CDT 2015 qmail-antispam : Revisando logs