Show journal logs from the time a service was restarted

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.
I came up with:
$ unit=prometheus
$ sudo systemctl restart $unit
$ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
$ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"
This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.
journalctl systemctl
add a comment |
Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.
I came up with:
$ unit=prometheus
$ sudo systemctl restart $unit
$ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
$ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"
This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.
journalctl systemctl
add a comment |
Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.
I came up with:
$ unit=prometheus
$ sudo systemctl restart $unit
$ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
$ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"
This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.
journalctl systemctl
Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.
I came up with:
$ unit=prometheus
$ sudo systemctl restart $unit
$ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
$ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"
This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.
journalctl systemctl
journalctl systemctl
asked Mar 17 at 16:09
SiddharthaSiddhartha
82
82
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use the invocation id, which is a unique identifier for a specific run of a service unit.
It was introduced in systemd v232, so you need at least that version of systemd for this to work.
To get the invocation id of the current run of the service:
$ unit=prometheus
$ systemctl show -p InvocationID --value "$unit"
0e486642eb5b4caeaa5ed1c56010d5cf
And then to search journal entries with that invocation id attached to them:
$ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf
I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)
Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.
1
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
add a comment |
While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.
I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.
status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2";
start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506833%2fshow-journal-logs-from-the-time-a-service-was-restarted%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the invocation id, which is a unique identifier for a specific run of a service unit.
It was introduced in systemd v232, so you need at least that version of systemd for this to work.
To get the invocation id of the current run of the service:
$ unit=prometheus
$ systemctl show -p InvocationID --value "$unit"
0e486642eb5b4caeaa5ed1c56010d5cf
And then to search journal entries with that invocation id attached to them:
$ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf
I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)
Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.
1
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
add a comment |
You can use the invocation id, which is a unique identifier for a specific run of a service unit.
It was introduced in systemd v232, so you need at least that version of systemd for this to work.
To get the invocation id of the current run of the service:
$ unit=prometheus
$ systemctl show -p InvocationID --value "$unit"
0e486642eb5b4caeaa5ed1c56010d5cf
And then to search journal entries with that invocation id attached to them:
$ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf
I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)
Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.
1
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
add a comment |
You can use the invocation id, which is a unique identifier for a specific run of a service unit.
It was introduced in systemd v232, so you need at least that version of systemd for this to work.
To get the invocation id of the current run of the service:
$ unit=prometheus
$ systemctl show -p InvocationID --value "$unit"
0e486642eb5b4caeaa5ed1c56010d5cf
And then to search journal entries with that invocation id attached to them:
$ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf
I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)
Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.
You can use the invocation id, which is a unique identifier for a specific run of a service unit.
It was introduced in systemd v232, so you need at least that version of systemd for this to work.
To get the invocation id of the current run of the service:
$ unit=prometheus
$ systemctl show -p InvocationID --value "$unit"
0e486642eb5b4caeaa5ed1c56010d5cf
And then to search journal entries with that invocation id attached to them:
$ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf
I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)
Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.
answered Mar 17 at 22:19
filbrandenfilbranden
10.8k21848
10.8k21848
1
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
add a comment |
1
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
1
1
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Thanks @filbranden! This look good.
– Siddhartha
Mar 17 at 23:05
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?
– Siddhartha
Mar 17 at 23:10
add a comment |
While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.
I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.
status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2";
start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";
add a comment |
While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.
I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.
status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2";
start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";
add a comment |
While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.
I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.
status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2";
start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";
While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.
I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.
status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2";
start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";
answered Mar 25 at 0:46
SiddharthaSiddhartha
82
82
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506833%2fshow-journal-logs-from-the-time-a-service-was-restarted%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown